UITableView crashes when datasource is connected in Interface Builder - iphone

- (NSInteger)numberOfSectionsInTableView:(UITableView *)cijferTableView{
return 1;
}
- (NSInteger)cijferTableView:(UITableView *)cijferTableView numberOfRowsInSection:(NSInteger)section {
return [marksArray count];
}
- (UITableViewCell *)cijferTableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [marksArray objectAtIndex:indexPath.row];
return cell;
}
I have a marksArray which is filled with strings.
The code worked fine until a quarter of an hour ago but since then it has been crashing when I load the view this code is in, without me changing anything.
When I, in interface builder, disconnect the datasource however, the view is loaded properly without a crash. But of course, it won't fill the table in that case.
What did I do wrong?
Update:
The error the console gives is terminate called after throwing an instance of 'NSException'
Also, i didnt exactly add anything into marksArray just yet. To test, i just have this:
//.h
NSMutableArray *marksArray;
and
//.m
marksArray = [NSMutableArray arrayWithObjects:#"1", #"2", nil;

It looks like you did a search and replace for "tableView" with cijferTableView and in doing so you renamed the methods, which will cause this to break. For example:
- (NSInteger)cijferTableView:(UITableView *)cijferTableView numberOfRowsInSection:(NSInteger)section {
return [marksArray count];
}
should be...
- (NSInteger)tableView:(UITableView *)cijferTableView numberOfRowsInSection:(NSInteger)section {
return [marksArray count];
}

1) You forgot to retain marksArray
2) Weird names for dataSource methods ('cijfer' stuff instead of tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath:). They will not work.

Why are you renaming your delegate methods? Maybe those are causing some of your problems?

Related

SearchBar does not work anymore after adding new objects

I have integrated a searchBar in my application. And it worked fine. But after adding new elements to my tableView my searchBar does not work anymore. I get error messages in this code block:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Here i get: >Control reaches end of non void function<
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if (searching)
{
VerwaltungInformation *searchedFormel = [copyListOfFormularies objectAtIndex:indexPath.row] ; //Here i get: >Thread 1: Program received signal "SIGABRT"<
cell.textLabel.text = searchedFormel.nameFormel;
}
else
{
NSDictionary *dictionaryCell = [listOfFormularies objectAtIndex:indexPath.section];
NSArray *arrayCell = [dictionaryCell objectForKey:#"Formel"];
VerwaltungInformation *cellValue = [arrayCell objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue.nameFormel;
}
return cell;
There seems to be a problem with the cellIdentifier - but I can not figure it out.
Thanks for any help!
The warning Control reaches end of non void function comes when you wrapped up your non void method without returning an object. To figure out your problem, right click your mouse select Structure select Re – Indent. Now you can find out the structure of your code more easily and find out what is happening.
I suspect the problem might be earlier in the source file, above the method you posted. Please try this:
Step 1:
#implementation MyClass
#synthesize ...
#if 0
// all of the code that precedes cellForRowAtIndexPath
#endif
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// and so on
Does the compiler still warn on the CellIdentifier? My guess is no (though you'll probably see all kinds of errors below, related to symbols that you hid inside #if #endif).
Step 2:
Move the #if #endif pair to wrap methods one at a time in the file, method by method, starting with the method above the one you posted, until the CellIdentifier warning reappears. When it does, you'll have found the source of the problem.

How to show text in a UITableView

I have a table and I want to show there some text; but I can't, I don't know why. My app is a view-based app, and I don't want to change it for a Table View Controller. Here's the code:
.h
{
NSArray *array;
IBOutlet UITableView *table;
}
.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
// Configure the cell
return cell;
}
Please help me because I'm trying to solve this and I don't know what's wrong, why the Table doesn't show the array!
Thank you!
You almost certainly haven't connected your datasource and delegates properly in IB.
Your UIViewController needs to implement UITableViewDelegate and UITableViewDatasource, and you need to then connect it to your UITableView (table) as such in Interface Builder.
For details, read through Apple's documentation here.
On your .H file are you using this interfaces <UITableViewDelegate, UITableViewDataSource>?
The answer is that you've never ever created single cell. The call you make dequeueReusableCellWithIndentifier only recycles cells you've already created. Add this code after the dequeueResusable... line:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

UITableView scrolling crashes app

When i scroll UITableView it crashes the app. Here is code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *tempDict = [albums objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"ImageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
AsyncImageView *oldImage = (AsyncImageView *)[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
I set the breakpoints and it stops at line
NSDictionary *tempDict = [albums objectAtIndex:indexPath.row];
What am i doing wrong?
Thanks in advance.
I am not sure, first of all please check the total number of rows and your array count. They must be same. If still it crashes then put this line
NSDictionary *tempDict = [[albums objectAtIndex:indexPath.row] copy];
Check size of albums array. May be it is smaller then indexPath.row or it was released somewhere.
Make sure you are not reading out of bounds of your NSdictionary.
Check your return value in the tableView:numberOfRowsInSection: dataSource method.
The most likely reason for a crash on that line is that your table view has more rows than you have items in your albums array. Normally, in a setup like this, you'd have exactly the same number of rows in your table, and your implementation of –tableView:numberOfRowsInSection: would look like this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// assuming that there is only one section
return [albums count];
}
I solved this problem. Actually in the $cellForRow ,I was trying to get information from server, I did not apply lazy loading there. Because of that while scrolling the tableView, app was crashing.
Thank you to all for your help.

iPhone: How to add rows in tableview after tabelview already loaded?

I have implemented "Add to Favourites" functionality for my iPhone application. It works fine except adding cells into my Favourite Table View during runtime. For example, I have given following methods.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
tableView.hidden = YES;
warningLabel.hidden = YES;
// Load any change in favourites
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:kFavouriteItemsKey];
self.favourites = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if([self.favourites count] > 0)
tableView.hidden = NO;
else
warningLabel.hidden = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.favourites count]; // Favorites is a dictionary contains required data
}
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:#"index: %d",indexPath.row];
return cell;
}
This code works fine and display rows correctly for the first time only! After tableview is loaded and if I add new item(s) in favorites or delete any item(s), it doesn't make any difference to my tableview! I want to display exactly what is available in Favourites dictionary. It seems CellForRowAtIndexPath doesn't get invoked when ViewAppear again. Is there any method for TableView that I can use to achieve my requirements?
I think you've missed to call [tableView reloadData];
The very easy way is to just call [tableView reloadData] whenever you make any changes.
There is also a better (faster for large tables, and possibly animated; more elegant), but much more complicated way which I won't go into unless you decide the reloadData way isn't sufficient for you.

iPhone - Array to UITableView

I can't display array in UITableView. The thing I do is - in viewWILLappear I'm creating array. In viewDIDappear I'm filling the array. But when I run [myArr count] or [myArr objectAtIndex:indexPath.row] in table setup I get empty table. If I define constant integer as row count and some constant string as cell text everything works fine. Is there some populate() method I have to run or is it a problem with some order of declarations?
Thanks for any help. Here's the code:
- (void)viewWillAppear:(BOOL)animated {
myArr = [[NSMutableArray alloc] initWithCapacity:100];
}
- (void)viewDidAppear:(BOOL)animated {
[self load_array];
}
- (void) load_array {
for (SomeObject *someObject in SomeObjects) {
[myArr addObject:someObject.someString];
NSLog(#"Value: %#", [myArr objectAtIndex:([myArr count]-1)]); // works
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myArr count]; // works if I return const ("return 2")
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [myArr objectAtIndex:indexPath.row]; //=#"ASDF" works.
return cell;
}
You need to perform reloadData on your table view to make the view re-load the table cells.
Update: You should not allocate your array in the viewWillAppear, as this method might be called several times. Construct the array in the viewDidload: and fill it there, or in a background thread, or in the viewWillAppear: (using a conditional statement to check if its already filled). You should also make sure that you do not create a memory leak, from the code you provided it is likely that myArr will be replaced by a newly allocated array without being released.