24.tableview注意事项

// tableview滚动到顶部的最佳方法
// ([self.tableV scrollToTop]; 有时无效),另外预估高度也可能会是tableview不能完全滚动到顶部
[self.tableV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
// cell使用masonry布局时,不要使用预估高度, 代理方法和属性都不要设置预估高度
- (UITableView *)tableV{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableV.delegate = self;
        _tableV.dataSource = self;
        _tableV.mj_header = self.refreshHeader;
        _tableV.mj_footer = self.refreshFooter;
        // 使用masonry布局cell时,不要使用预估高度,会冲突
//        _tableV.estimatedRowHeight = 0;
//        _tableV.estimatedSectionHeaderHeight = 0;
//        _tableV.estimatedSectionFooterHeight = 0;
        _tableV.contentInset = UIEdgeInsetsMake(kFitWidth(10), 0, kTabBarSafeH + kFitWidth(44), 0);
        _tableV.backgroundColor = [UIColor nf_bgColor];
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableV;
}

   // 有没有数据都要刷一次最新的
        [self.tableV reloadData];

      // 不要在reloadData的时候立刻执行滚动操作,这样会导致无法完全滚动到顶部
      // 因为reloaddata是需要时间的,所以可以等主线程空闲的时候再执行滚动操作,这样就不会相互干扰, 先刷新,在滚动
        dispatch_async(dispatch_get_main_queue(), ^{
            if (self.dataSource.count > 0) {
                // 最后参数值为YES,有滚动到顶部的过渡动画效果。([self.tableV scrollToTop]; 有时无效)
                [self.tableV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
            }
        });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容