UITableView reloadData does not seem to cause cellForRowAtIndexPath: to be called
Solution 1
When things like this happen to me, it's usually because I'm calling -reloadData
outside of the main thread.
Solution 2
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.
Solution 3
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!
Solution 4
Make sure you call performSelectorOnMainThread with waitUntilDone:NO, otherwise you may still have the same issue
Solution 5
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.
![Fritzables](https://i.stack.imgur.com/vFQEo.png?s=256&g=1)
Fritzables
Updated on July 13, 2022Comments
-
Fritzables almost 2 years
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?
-
Bart Jacobs over 10 yearsThis is something so easily overlooked especially when working with completion handlers invoked from background threads. Excellent tip.
-
olivaresF over 10 yearsFor some reason, this actually fixed it for me. That's wrong!
-
Grey Code over 10 years@OlivaresF - Thanks, this worked for me, moves inside cellForRowAtIndexPath. but not refreshing in TableView.
-
srinivas almost 10 yearsindexSetwithindex methods are no longer available in swift, and i am facing this same reloaddata problem
-
jbcaveman almost 10 yearsBanged my head on this for hours. Finally got it to work after reading this. Thanks. Be careful of completion blocks like Bart says. Make sure you call [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]
-
Max Friedman almost 9 yearsthe only thing that worked for me after days of headaches. WHY XCODE WHY