John Anthony John Anthony - 1 year ago 294
Objective-C Question

numberOfRowsInSection: not called on reloadData

I have a UITableView that uses an array to list data. This works fine.
I also have an UISearchBar for searching in that tableview. When data is matched in the tableviews array those rows are added to another mutable array, and cellForRowAtIndexPath: displays data from that mutable array instead.

But numberOfRowsInSection: is never called when I call reloadData. Therefore the tableview crashes when scrolling, because it's trying to get data from the mutable array, but for rows that are in the original array (which has more items)

I've debugged for quite a while now, but can not for the love of god find the reasons for this. Datasource and Delegate are hooked up in the tableview, because it can show the data from the original array.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
NSLog(@"isSearching: %d", isSearching);
// Return the number of rows in the section.
return [originalArray count];

NSLog(@"isSearching - Return searchCopyListOfItems");
return [searchCopyListOfItems count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

cell.textLabel.text = [originalArray objectAtIndex:indexPath.row];
cell.textLabel.text = [searchCopyListOfItems objectAtIndex:indexPath.row];

return cell;

Answer Source

how you reload your table? doing [table relaodData]; right? is your table is connected to IBOutlet on your nib? at the point of reloading , is table initialized?

you can post crash info too.