4

Possible Duplicate:
Get notified when UITableView has finished asking for data?

is it possible to get notification when tableview finish to reload its data after reloadData method? Or is it possible to wait till tableview finish reloading?

Thanks

Community
  • 1
  • 1
Michal
  • 43
  • 1
  • 1
  • 3

2 Answers2

2

There is a very simple solution is to check if you are in the last iteration of the delegate

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 

after last iteration finished in the end add your code there

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == lastRow){
          any code you want

    }
}

Thanks

codercat
  • 22,873
  • 9
  • 61
  • 85
user784625
  • 1,928
  • 5
  • 24
  • 38
  • Thx, it works after small modification. – Michal Jun 27 '11 at 10:58
  • 20
    Have you considered the following? 1) According to `UITableView`'s documentation, when `reloadData` is called, "the table view redisplays only those rows that are visible." So the possibility exists that `tableView:willDisplayCell:` won't be called on the last row. 2) Even though irrelevant to this question, if the table view has a footer, it will probably be loaded after the cells have been loaded. Furthermore, `reloadData` may do even more stuff after loading the footer, in which case we won't know _exactly_ when `reloadData` finished. –  Nov 04 '11 at 23:54
  • How to take care of this when the bottom row is not visible because it is still off screen and there is no footer. My view only holds 10 rows and there's actually 20 (so 10 off screen). I want to scroll to the bottom one. – Rutger Huijsmans Aug 15 '16 at 05:13
1

Lets put a completion block in. Who doesn't love a block?

@interface DUTableView : UITableView

   - (void) reloadDataWithCompletion:( void (^) (void) )completionBlock;

@end

and...

#import "DUTableView.h"

@implementation DUTableView

- (void) reloadDataWithCompletion:( void (^) (void) )completionBlock {
    [super reloadData];
    if(completionBlock) {
        completionBlock();
    }
}

@end

Usage:

[self.tableView reloadDataWithCompletion:^{
                                            //do your stuff here
                                        }];

EDIT:

Note that the above is only half the solution. The number of sections and rows and the row heights are updated synchronously on the main thread, so using the above code, the completion would call back when some of the UI part was done. But the data loading from the datasource is asynchronous - so really this answer isn't quite what @Michal wanted.

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
  • 6
    Doesn't this just suffer the same problem as waiting for `reloadData` to finish? The problem is that the updating of the table happens asynchronously and will be continuing after `[super reloadData]` is done. – Rob Jan 09 '13 at 00:03
  • Yeah, you're right. The number of sections and rows and the row heights are updated synchronously, so part of the call is synchronous. But the data loading from the datasource is asynchronous. I'll update my answer so as not to mislead others. – bandejapaisa Feb 04 '13 at 23:36