简体   繁体   中英

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;
    }

if numberOfSections returns 0 , numberOfRowsInSection will not be called.

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.

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.

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.

在此处输入图像描述

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

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.

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

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

ie 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!

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.

Try

[self.tableView reloadData];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM