在表格视图中,我必须滚动到顶部。 但是我不能保证第一个对象将是第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)
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3166315