简体   繁体   中英

cellForRowAtIndexPath not called even when [objects count] in numberOfRowsInSection shows 46 objects

About to pull my hair on this one. My tableview stopped loading the data all of a sudden. I have breakpoints in numberOfRowsInSection and cellForRowAtIndexPath . numberOfRowsInSection which goes like

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [objects count];
}

gets called thrice, for some reason, when the view initially loads with the number of objects in the objects array being 0 which can be understood because the tableview doesn't have any data initially.

After I fetch data from the server and store those objects in my objects array and call [self.tableView reloadData]; , numberOfRowsInSection gets called again with the number of objects in objects array this time being 46 but it still doesn't go ahead and enter cellForRowAtIndexPath for some reason.

I have tried reloading the data on the main thread -

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
});

No use.

I have checked all my spellings.

Checked that my UITableView object is not nil when I am reloading the data.

The frame of the table view is 320.0000*568.0000 in viewDidLoad .

No idea what's going wrong. And I am assuming I broke something because the data was loading just fine earlier.

I am assuming that you are loading the view from another view, then you should call [tableView reloadData] on ViewDidLoad also update dataSource array there . This is because of the view life cycle - when you are coming back from one view and your tableView data source is updated, the view life cycle will be like this:

  1. viewDidLoad
  2. tableViewDelegateMethods
  3. viewDidAppear

So when you update tableView's data source object and call reloadData during viewDidLoad will resolve the issue.

[self.tableView beginUpdates];

I have no idea when and why I added this and then forgot to call endUpdates . Sigh.

Thanks community. Huge help!

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