objective c - How to tell when UITableVIew has completed ReloadData? -
i trying scroll bottom of uitableview after done performing [self.tableview reloaddata]
i had
[self.tableview reloaddata] nsindexpath* indexpath = [nsindexpath indexpathforrow: ([self.tableview numberofrowsinsection:([self.tableview numberofsections]-1)]-1) insection: ([self.tableview numberofsections]-1)]; [self.tableview scrolltorowatindexpath:indexpath atscrollposition:uitableviewscrollpositionbottom animated:yes];
but read reloaddata asynchronous, scrolling doesn't happen since self.tableview
, [self.tableview numberofsections]
, [self.tableview numberofrowsinsection
0.
thanks!
what's weird using:
[self.tableview reloaddata]; nslog(@"number of sections %d", [self.tableview numberofsections]); nslog(@"number of rows %d", [self.tableview numberofrowsinsection:([self.tableview numberofsections]-1)]-1);
in console returns sections = 1, row = -1;
when exact same nslogs in cellforrowatindexpath
sections = 1 , row = 8; (8 right)
the reload happens during next layout pass, happens when return control run loop (after, say, button action or whatever returns).
so 1 way run after table view reloads force table view perform layout immediately:
[self.tableview reloaddata]; [self.tableview layoutifneeded]; nsindexpath* indexpath = [nsindexpath indexpathforrow: ([self.tableview numberofrowsinsection:([self.tableview numberofsections]-1)]-1) insection: ([self.tableview numberofsections]-1)]; [self.tableview scrolltorowatindexpath:indexpath atscrollposition:uitableviewscrollpositionbottom animated:yes];
another way schedule after-layout code run later using dispatch_async
:
[self.tableview reloaddata]; dispatch_async(dispatch_get_main_queue(), ^{ nsindexpath* indexpath = [nsindexpath indexpathforrow: ([self.tableview numberofrowsinsection:([self.tableview numberofsections]-1)]-1) insection:([self.tableview numberofsections]-1)]; [self.tableview scrolltorowatindexpath:indexpath atscrollposition:uitableviewscrollpositionbottom animated:yes]; });
update
upon further investigation, find table view sends tableview:numberofrowsinsection:
data source before returning reloaddata
. if delegate implements tableview:heightforrowatindexpath:
, table view sends (for each row) before returning reloaddata
.
however, table view not send tableview:cellforrowatindexpath:
until layout phase, happens default when return control run loop.
and find in tiny test program, code in question scrolls bottom of table view, without me doing special (like sending layoutifneeded
or using dispatch_async
).
Comments
Post a Comment