iOS TableView设置contentOffset 滚动
2018-10-22 本文已影响0人
__May__
由于需要让tableView跳转到最顶端的位置,所以设置了tableView的contentOffset为(0,0)。这时候突然发现当我首次加载的数据时 是可以正常回到顶部的,分页之后 第一次设置时 tableView的内容并没有回到顶部 ,而是可变性的回到了中间靠上的位置 但在第二次的时候就可以成功回到顶部了。
解决办法:
self.tableView.estimatedRowHeight= 0;
self.tableView.estimatedSectionHeaderHeight= 0;
self.tableView.estimatedSectionFooterHeight= 0;
完美解决
原因:
当tableView的Cell数量改变后再次reload,contentOffset的值是通过预估各cell的高度及header、footer的高度后计算得到的,并非准确的值。知道原理后,解决办法也就简单了,关闭系统自带的预估就好了
estimatedRowHeight是一个预估高度,iOS11之前是为0,在iOS11下,这个值默认为44
具体原因如下 摘自http://www.cocoachina.com/ios/20171109/21103.html


