开发者

Make UITableView go to top line

I sometimes reload table data and I need to redraw the table so that I see the data from the first row again. What I do now is the following:

NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionTop ];
[self.tableView deselectRowAtIndexPath:ip animated:NO]; 

This is not ideal because ther开发者_运维技巧e is an annoying flash when row 0 is selected and immediately deselected.

How can I fix this?


You can use UITableView's scrollToRowAtIndexPath:atScrollPosition:animated: method.

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];


If you have a header that is in view by default, and want to compensate if the action is called when the list is in motion use:

[tableview setContentOffset:CGPointMake(0.0, 0.0) animated:NO];

Short, simple, effective.


You can set contentoffset of tableview to (0.,0.).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜