uitableview reloadData - how do we table refresh is done? - uitableview

I want to do a reloadData on a UITableView and after the table refresh is done, I want a delegate method to be called when I can know that the refresh is done. How do we know that ?

-reloadData blocks execution—your code won't continue past that call unless you're doing it on a separate thread. It should only be configuring nine cells or so at most; is there some reason you expect that to take a long time?

Related

visibleCells are empty after reloadData/invalidateLayout - how to retrieve the visible cells at the correct time

I want to access the visibleCells in scrollViewDidEndDecelerating, but I get an empty array. The reason is that I called reloadData and invalidateLayout shortly before in viewWillLayoutSubviews and it seems that both functions (scrollViewDidEndDecelerating and viewWillLayoutSubviews) are asynchronous. Even if want to get the visible cells, right after calling reloadData I get an empty array. So it has not been finished recalculating everything.
How do I know at which time/which method there are valid visible cells? Do I have to force everything running on the main UI thread or how do I run that in sync?
Edit:
My solution is to use another approach and don't use the visible cells for this.

UITableView does not respond normally when I scroll during loading

I have come across a very tedious problem. When I try to load data asynchronously and then finally call -reloadData the tableview does not decelerate upon a scroll event and does not respond to touch events. How can I prevent this from happening ?
There is a table view with some data in it. The user is manipulating the table as new data comes in asynchronously. The proper way to modify the table is to not use reload data, but to use the "insert..." methods, which leave existing cells alone.

When is tableView:numberOfRowsInSection: called in UITableView?

tableView:numberOfRowsInSection is sent to the delegate of a UITableView to find out how many rows it needs to have in a given section.
My question is, when and how often is this method called?
The method is called very first time the tableview is getting loaded and if you are more interested in the delegates then put a breakpoint and check when and where which delegate is called and how many times.
Below are the instances when that function will get called,
For the first time when table is loaded
the time you reload the table data
the time you add/update/delete your row or sections dynamically.
The method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section is a protocol method of the UITableViewDataSource - protocol. It will be called
the very first time your table view is loaded based on that you have set the dataSource properly, e.g.
self.yourTableView.dataSource = self;
If you are interested in updating your table again at a later time you can call
[self.yourTableView reloadData];
in order to reload the entire table. If you are only interested in reloading a part of your table you can do something similar to
NSIndexSet *reloadSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSectionsInTableView:self.yourTableView])];
[self.yourTableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic];
Hope it helps!
My question is, when and how often is this method called?
Short Answer : When your UITableView needs to update something.
Long Answer : Delegates Methods generally called themselves however it may be called multiple times when your UITableView needs to update something. By default, it's called very first time the tableview is getting loaded or updated (reloaded).
It depends on how often user will scroll UITable view to section and how many sections there are. This value, which is returned by this function and is casched. Method will need be revoked if you will update content of table view (filtering results, or updating data via reloadData).
Best thing for you will be to add logging to this function and check this yourself.

Is -[UITableView reloadData] asynchronous or synchronous?

When you call -reloadData on a table view instance, does it make all UITableViewDataSource calls async?
Thank you.
This method actually just removes all the UITableViewCell views from the table. The data source delegate methods are called when the table is repainted.
So, it's asynchronous.
Edit:
Actually, some calls ARE synchronous. The number of sections & rows and row heights are updated immediately, so, for example contentSize is set correctly after the call. On the other hand, tableView:cellForRowAtIndex: is not called until the table is repainted.

UITableView reloadData fires only once

I have a TabBarController with two tabs SCHEDULE and DATE RANGE
When the view loads I'm hitting a webservice to collect the end users Schedule for that day. After parsing the XML I call ScheduleTable reloadData and all is well. I have tons of NSLog statements to follow the performance.
Clicking the DATE RANGE tab displays a DatePicker. After selecting the date range I have a button which when clicked passes the dates to the webservice again. In my log I can see the new dataset and can verify I have the data I need, when the code reaches the reloadData line, it does not fire the UITableView reload methods.
Any help is appreciated. I've tried viewWillAppear and self.ScheduleTable reloadData, neither have helped.
Check to see if you've connected the UITableView to the ScheduleTable IBOutlet in Interface Builder.
You say that the first refresh is working. That may be because it is automatically done by the tableview.
I understand that the second time you are calling reloadData programatically. If you haven't connected the IBOutlet, then your ScheduleTable variable is nil and [ScheduleTable reloadData] will do nothing.
when the code reaches the reloadData line, it does not fire the UITableView reload methods. Any help is appreciated. I've tried viewWillAppear and self.ScheduleTable reloadData
Just before saying [self.scheduleTable reloadData] (never start an ordinary variable name with a capital letter), log the table:
NSLog(#"%#", self.scheduleTable);
I'm betting it won't be the table and that you're accidentally sending this message into empty air.

Resources