iOS数据库指南iOS 坑的集中营

解决scrollToRowAtIndexPath:滚动位置偏差问

2018-10-20  本文已影响76人  爱迪生的小跟班

场景:
在TableView中有多个section,每个section中有多个cell;此时要滚动到某个section的第一行,使用如下方法:

NSIndexPath * dayOne = [NSIndexPath indexPathForRow:0 inSection:index];
[myself.tableView scrollToRowAtIndexPath:dayOne atScrollPosition:UITableViewScrollPositionTop animated:YES];

然而,滚动的时候存在位置偏差问题,滚动范围较大的时候,位置不是很正确,总是偏差那么一点。

尝试延迟加主线程滚动:

dispatch_after(0.2, dispatch_get_main_queue(), ^{
                        NSIndexPath * dayOne = [NSIndexPath indexPathForRow:0 inSection:index];
                        [myself.tableView scrollToRowAtIndexPath:dayOne atScrollPosition:UITableViewScrollPositionTop animated:YES];
                    });

还是同样问题。。。

解决办法:

预估tableview的cell高度

//增加下面三句(高度预算),解决scrollToRowAtIndexPath 滚动到指定位置偏差不准确问题
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;

在tableview初始化的时候,进行高度预估,加入上面三句。完美解决。

感谢阅读 , -松小宝-

上一篇下一篇

猜你喜欢

热点阅读