Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop UITableView from loading until data has been fetched?

I understand that UITableView will call -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method to get each of the cells for the table view. Say I have my data source is fetched over the internet and I have to account for latency. What will be the best way of "stopping" this method from being called? Should it block on a boolean flag in the application? Should I just call cellForRowAtIndexPath again from within my application?

I am uncertain as to when the function gets called, ie, how often the UITableView "refreshes" itself. Any explanations will be helpful! Thanks!

like image 451
Ying Avatar asked Feb 16 '10 19:02

Ying


People also ask

How will you know the end of loading of UITableView?

willDisplayCell: used here for smoother UI (single cell usually displays fast after willDisplay: call). You could also try it with tableView:didEndDisplayingCell: . Much better for knowing when all the cells load that are visible. However this will be called whenever the user scrolls to view more cells.

What does tableView reload data do?

reloadData()Reloads the rows and sections of the table view.

What is tableView data source?

Overview. Table views manage only the presentation of their data; they don't manage the data itself. To manage the data, you provide the table with a data source object — an object that implements the UITableViewDataSource protocol. A data source object responds to data-related requests from the table.

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.


1 Answers

If you don't have data, or you don't have the data for additional cells, then cellForRowAtIndex: will not be called as long as you don't tell the UTableView that you have rowCounts or new rowCounts. That value is being set in numberOfRowsInSection:.

In other words, don't report any new cells in numberOfRowsInSection:, until you actually have that data in hand, and then cellForRowAtIndexPath: won't be called prematurely.

When you do get the additional row data, then call reloadData to get the UITableView to ask for the number of rows and then call cellForRowAtIndex:.

like image 111
mahboudz Avatar answered Sep 28 '22 03:09

mahboudz