美文网首页
UITableView静态刷新跳动

UITableView静态刷新跳动

作者: 守护地中海的花 | 来源:发表于2020-03-12 10:30 被阅读0次

    现象

    当UITableView滑动范围大于UITableView自身的时候 ,滑动到最底部,点击cell 刷新 会出现跳动,系统不一样跳动高度也不一样测试看看

    复现 iOS13.1 iphone7

    • 设置UITableView高度离购物车按钮还要20px距离


      iOS13有跳动.gif
    • 设置UITableView高度离购物车按钮0px


      iOS13无跳动.gif

    复现 iOS12.4.5 iphone6

    iOS12跳动很大.gif

    参考

    iOS TableView reloadData刷新列表cell乱跳iOS11 UITableView reloadData 界面跳动问题

    原因:

    产生的原因是在创建TableViewCelll的时候,系统给加了一个默认预估estimatedRowHeight的cell高度== UITableViewAutomaticDimension

    参见系统属性备注:[@property](https://my.oschina.net/property) (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // default is UITableViewAutomaticDimension, set to 0 to disable 默认是UITableViewAutomaticDimension,当设置这个属性是0的时候,就不会估算cell高度了。

    iOS 11以后系统默认开启Self-Sizing,Self-Sizing官方文档解释:大概是说我们不用再自己去计算cell的高度了,只要设置好这两个属性,约束好布局,系统会自动计算好cell的高度,正是这个原因导致了高度计算出现问题,出现跳动现象

    解决方案

    tableView.estimatedRowHeight = 0;
    如果你有使用、加载sectionHeadView或sectionFootView的需求,也会出现闪屏现象,同理将这两个估算高度设置为0即可。

    if (@available(iOS 11.0, *))
    {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
    }
    else
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    

    或者协议方法

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 200*ADAPTER_WIDTH;
    }
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
    {
        return 50*ADAPTER_WIDTH;
    }
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section
    {
        return 0;
    }
    

    遗憾

    iOS12.4.5 iPhone滑动到底部还是有些跳动 只是跳动范围变小了

    相关文章

      网友评论

          本文标题:UITableView静态刷新跳动

          本文链接:https://www.haomeiwen.com/subject/lkhyjhtx.html