Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
         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;
    }
like image 960
John Anthony Avatar asked Jul 13 '12 10:07

John Anthony


Video Answer


1 Answers

if numberOfSections returns 0, numberOfRowsInSection will not be called.

like image 80
Timeless Avatar answered Nov 07 '22 03:11

Timeless