How to make tableView Scroll Automatically to top/

2019-02-11 08:51发布

This is just a test, which i might add it to the app. Well.... I have a tableView.

  1. If click button1, I want the tableView to scroll up automatically to the top.

  2. If I click button2 I want the tableView to scroll down automatically to the bottom.

How can this be done ? What about Scroll Speed ?

2条回答
beautiful°
2楼-- · 2019-02-11 09:30

You can also look at the scroll view's setContentOffset:animated: method.

Going to the top would mean,

[self.tableView setContentOffset:CGPointZero animated:YES];

and the bottom would be,

CGFloat height = self.tableView.contentSize.height - self.tableView.bounds.size.height;
[self.tableView setContentOffset:CGPointMake(0, height) animated:YES];

You won't have control over the animation duration.

The only problem with scrollToRowAtIndexPath:atScrollPosition:animated: is that it might not be helpful when section and table headers are set.

查看更多
smile是对你的礼貌
3楼-- · 2019-02-11 09:47

scrollToRowAtIndexPath:atScrollPosition:animated:

Scrolls the receiver until a row identified by index path is at a particular location on the screen.

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
查看更多
登录 后发表回答