1.UITableView
自带方法滚动到某组某行:
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UIViewTintAdjustmentModeAutomatic animated:YES];
注意
:可能会有数组越界奔溃的情况(如有有section但没有cell的情况),不能保证的情况下不要使用。
2.UITableView
继承UIScrollView
,可以调用UIScrollView
滚动到设定区域的的方法
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 0.1, 0.1) animated:YES];
3.设置偏移量
[self.tableView setContentOffset:CGPointZero animated:YES];
也可以CGPointMake(0, 0)
问题:第2种和第3种方法可能会出现顶部空白的情况如下图所示:
滚动到顶部空白
有人说是线程异步的问题让用回主线程异步执行上面设置滚动方法或偏移量,然而并没有用,而用第1种方法却没有以上问题,所以最终选择了一下方法:
- (void)scrollToTop{
CommissionDetailModel * model = self.dataSource.firstObject;
if (model&&model.rows) {//验证一下,保证不会数组脚标越界
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}else{
[self.tableView setContentOffset:CGPointZero animated:YES];
[self performSelector:@selector(reload) withObject:nil afterDelay:0.3];
}
// dispatch_async(dispatch_get_main_queue(), ^{
// [self.tableView setContentOffset:CGPointZero animated:YES];
// });
//并没有效果
}
- (void)reload{
[self.tableView reloadData];
}