简体   繁体   中英

UITableView reloadData does not seem to cause cellForRowAtIndexPath: to be called

I am currently writing an app, using storyboards, that contains two UITableView s within the same window.

When running the application, the first UITableView will populate with a number of "Registration Numbers".

When the user taps on a cell, then the second UITableView is supposed to populate with detail about the selected cell.

When tapping a number in the first table I have a method that drives the:

[mySecondTableView reloadData]

Which it does. I am under the impression that when invoking the reloadData command, it should then call both:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

and

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

The first fires, but the second won't. I have both the data source and delegate wired to self.

I am not sure what I am missing.

Are both methods meant to fire?

当发生这种情况时,通常是因为我在主线程之外调用了-reloadData

After reloadData, try this:

NSIndexSet * sections = [NSIndexSet indexSetWithIndex:0];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationNone];

I found if the data change is subtle, the table view seems to optimize away the need to refresh.

Pointed answers doesn't solve my table view problem. It was still calling

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

however cell drawing method wasn't fired:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

My problem was that(because of my mistake of course) table view height was 0. So as table displays only cells that are visible, it never tries to display a cell. When I changed height value drawing cell method started to fire again.

Botom line double check your table frame and ensure you have non-zero width and height values!

I just incurred this same issue. I used a performSelectorOnMainThread to call a method which then calls the reloadData . This was necessary since I was trying to update outside the main thread as suggested by hatfinch.

确保使用waitUntilDone:NO调用performSelectorOnMainThread,否则您可能仍然遇到相同的问题

Found another cause of cellForRowAtIndexPath not being called.

If numberOfRowsInSection returns 0, cellForRowAtIndexPath won't be called. This happened in my case because of an NSArray being de-allocated during a view controller change and its consequent count (since it was nil) returning 0.

After banging my head against a wall for days this solved my problem:

Set the All Exceptions breakpoint and see if you are getting an out of bounds exception in your datasource. Without setting the breakpoint there is no crash and the tableview will simply fail to reload.

Full answer here

I was debugging a similar issue for hours before I realized my .reloadData() call was being executed by a callback of a previous instance of the view controller.

So my breakpoint was hitting on the .reloadData() call of my old instance of that view controller, but the new instance that was actually shown wasn't reloading because it wasn't the one executing the callback that called .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