views:

125

answers:

2

Hi, I understand that UITableView will call (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 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!

A: 

What you should do is call reloadData on your table view when your data is done being fetched. That will force the table view to call it's delegate methods again for display. Those methods are called whenever the table view needs to re-display, so either when it comes into view, scrolling occurs, or you manually call reloadData.

Greg Martin
+1  A: 

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:.

mahboudz
thanks for this answer. how would you set numeberOfRowsInSection later? when reloadData is called, will cellForRowAtIndex or numberOfRowsInSection be called automatically?
Ying
Yes, when you call reloadData, many of these calls will be made automatically.
mahboudz