美文网首页实用UITableVIew
UITableView - scroll to the top(

UITableView - scroll to the top(

作者: 952625a28d0d | 来源:发表于2015-11-23 17:33 被阅读3859次

In my table view I have to scroll to the top. But I cannot guarantee that the first object is going to be section 0, row 0. May be that my table view will start from section number 5.

So I get an exception, when I call:

[mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];```

Is there another way to scroll to the top of table view?

提问者的意思是它使用了如上代码想让TableView滚动到视图顶部,但是不起作用,求解决办法。

解决办法:

1:UITableView is a subclass of UIScrollView, so you can also use:
UITableView是UIScrollView的一个子类  所以可以用以下方法 

[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];```

或者

[mainTableView setContentOffset:CGPointZero animated:YES];```

Swift:

mainTableView.setContentOffset(CGPointZero, animated:true)```

相关文章

网友评论

    本文标题:UITableView - scroll to the top(

    本文链接:https://www.haomeiwen.com/subject/jterhttx.html