UITableView-滚动到顶部

蓝咒 提交于 2020-02-27 04:34:48

在表格视图中,我必须滚动到顶部。 但是我不能保证第一个对象将是第0节,第0行。可能是我的表视图将从第5节开始。

所以当我打电话时我得到一个例外:

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

还有另一种滚动到表格视图顶部的方法吗?


#1楼

此代码让您将特定部分滚动到顶部

CGRect cellRect = [tableinstance rectForSection:section];
CGPoint origin = [tableinstacne convertPoint:cellRect.origin 
                                    fromView:<tableistance>];
[tableinstance setContentOffset:CGPointMake(0, origin.y)];

#2楼

最好不要使用NSIndexPath(空表),也不要假设最高点是CGPointZero(内容插入),这就是我所使用的-

[tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];

希望这可以帮助。


#3楼

如果您想在表格中移动滚动动画,请使用此代码。 滚动将在0.5秒内移动到动画顶部。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

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

[UIView commitAnimations];

#4楼

迅速:

tableView.setContentOffset(CGPointZero, animated: true)

#5楼

斯威夫特:

如果您没有tableView标头:

tableView.setContentOffset(CGPointMake(0,  UIApplication.sharedApplication().statusBarFrame.height ), animated: true)

如果是这样的话 :

tableView.setContentOffset(CGPointMake(0, -tableViewheader.frame.height   + UIApplication.sharedApplication().statusBarFrame.height ), animated: true)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!