17
votes

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.
         if(!isSearching)
            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];
         }

         if(!searching)
             cell.textLabel.text = [originalArray objectAtIndex:indexPath.row];
         else
             cell.textLabel.text = [searchCopyListOfItems objectAtIndex:indexPath.row];

         return cell;
    }
9
Check if the table has memory when you are reloading it..Tripti Kumar

9 Answers

5
votes

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.

17
votes

if numberOfSections returns 0, numberOfRowsInSection will not be called.

10
votes

Be sure not to call reloadData from within any of the table view's delegate methods. This will cause unpredictable behavior. Also, make sure you did not call beginUpdates before, and that you only call your reloadData method from the main thread.

1
votes

I had the same issue today with XCode 6.1

In my code, I had the dataSource and delegate set:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

...and strangely, my code would populate the table when it first loaded... but it then ignored any calls to reloadData.

In the Storyboard, when I right-clicked on the Table View in the Storyboard, it's "outlet" radio button was an empty circle.

I dragged a line from this empty circle to the parent UIView, and then the reloadData did work okay.

enter image description here

Somehow, I had lost the IBOutlet link between my UITableView control and the .h/.m files used to populate it.

1
votes

Just also make sure you're not moving the table in the process of reloading data cause it might call the cellForRowAtIndexPath: while you're still updating your data.

0
votes

The problem for me was that the stubbed-out number of sections returned 0, and even though I wasn't planning to use sections, there's got to be 1 as it's higher in the object hierarchy than rows: https://stackoverflow.com/a/26632808/1449799

0
votes

For me, the gotcha was that I was attempting to increment a value and return it from numberOfSectionsInTableView

i.e. return someValue++

Apparently this isn't possible, and in my case, it wasn't incrementing someValue from 0 to 1, and so returning 0 sections, hence there was no reason for the numberOfRowsInSection: to be called!

0
votes

The solution for me was that I'd copy-pasted it from another nib and forgotten to rewire the dataSource and delegate outlets in IB. This has happened to me before but I thought I'd post it here in case this solution helps some who come across this problem.

-3
votes

Try

[self.tableView reloadData];