Reputation: 6846
[tableView reloadData]
does not call numberOfRowsInSection:(NSInteger)section
What i would write as an answer in stackoverflow if i saw a question like this:
I tried my answers before asking the question.
I put the following line in my MyTableView class (extended from UITableView)
- (void)reloadData{
NSLog(@"MyTableView (reloadData) self.dataSource: %@, delegate: %@, isMainThread: %d",self.dataSource,self.delegate,[NSThread isMainThread]);
[super reloadData];
[self.delegate performSelector:@selector(tableReloaded:) withObject:self];
}
I see that it is called from mainthread and datasource and delegate is never nil.
I am not curious about why tableView does not call cellForRow as it first has to call numberOfRows to see if the result > 0. However, it doesn't call numberOfRows too...
Any ideas? What can be the reason for tableView to giveUp calling numberOfRows function?
Update: I've put new lines to reloadData function to print FullStack to see if it is being called from tableView's own functions. The result: They are called outside of the tableView so it there shouldn't be any unexpected behaviour.
Update2: "Sometimes": I have discovered this problem after my users started to say that "sometimes" they don't see the changes in the table. After that, i tried to hit run button in XCode continuouslly until the app opens with this bug. (30% percentage of my runs shows this bug). When it happens, until i restart the application, reloadData never works again.
Update3:
I put self.dataSource==myExpectedViewController
and also [self.dataSource tableView:self numberOfRowsInSection:0]
to see if even delegates are not nil maybe they were being cloned etc.. The result was True and numberOfRows were correctly returning>0. So delegates are correct and i have more items than zero.
Update4: I tried it with a fresh UITableView (removing my custom UITableView), i got the same results.
Update5: I've put a button on the screen which recreates the table and sets its delegates. Whenever the problem in the question happens, i hit this button and everything starts to work perfectly. So, there must be something that breaks internals of UITableView which invalidates every call to reloadData, but i still couldn't find it.
Upvotes: 2
Views: 3384
Reputation: 11
Edited : I tried to replicate the issue and found, when I presented another viewcontroller on current view controller which has this tableview. and then 1. tried to reload this tableview with some data and 2. dismissed the viewcontroller which is on top of my currentviewcontroller with animation, simultaneously , then since in this duration, table view is not visible so it won't be reloaded.
To resolve, you can put a delay there or use completion block of that animation to reload table with effect.
Otherwise problem can be related to thread, UIElements are expected to be updated only on main thread.
During API calls and other process, we enters in other thread, and mostly we ignore to check whether we are on main thread before updating UIElement.
You should call yourTable.reloadData() in main thread using DispatchQueue.main.async{yourTable.reloadData()} and make sure it is being called on main thread.
Upvotes: 1