tableView 使用 reloadSections:withRowAnimation: 时,会跳动的问题

添加定时器两秒调用一次reloadSections:方法;
发生下面情况:刷新时 tableview 会跳动一下

clipboard.png

尝试过的方法:

    // 1
    [self.tableView beginUpdates];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
    
    // 2
    [UIView performWithoutAnimation:^{
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    }];
    
    // 3
    [self.tableView performBatchUpdates:^{
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    } completion:^(BOOL finished) {}];
    
    // 4
    [self.tableView beginUpdates];
    [self.tableView performBatchUpdates:^{
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    } completion:^(BOOL finished) {}];
    [self.tableView endUpdates];

均没效果

阅读 20.7k
3 个回答

iOS11默认开启Self-Sizing 如果你没用到预估高度 那么你尝试在Appdelegate.m中的didFinishLaunchingWithOptions方法中,加上如下代码,看看是否有效

if (@available(iOS 11.0, *)) {
        UITableView.appearance.estimatedRowHeight = 0;
        UITableView.appearance.estimatedSectionFooterHeight = 0;
        UITableView.appearance.estimatedSectionHeaderHeight = 0;
    }

建议去看看适配iOS11的相关内容

新手上路,请多包涵

设置预估行高、section头部高,section尾部高试一下?

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.delegate = self;
        self.dataSource = self;
        self.tableFooterView = [UIView new];
        self.estimatedRowHeight = 40.f ;
        self.rowHeight = UITableViewAutomaticDimension;
        self.estimatedSectionHeaderHeight = 50.f;
        self.sectionHeaderHeight = UITableViewAutomaticDimension;
        // 希望footer高为0
        self.estimatedSectionFooterHeight = 0.f;
        self.sectionFooterHeight = 0.f;
    }
    return self;
}

因为tableview的load和reload,是先根据预估行高做一个轮廓的搭建,再把自定义的数据填充进去做高度的微调。所以假如不做预先的设置,默认是根据UITableViewAutomaticDimension做预估行高的(好像是44),这样的渲染导致了界面抖动,甚至到时scrollVie上移或下移。

因此需要在init方法中设置 预估行高,
并尽量确保 预估行高 和heightForRowAtIndexPath、heightForHeaderInSection 、heightForFooterInSection中返回值保持一致。
这样做的好处是去除渲染时的界面抖动,同时提高界面渲染的性能。

这是没有设置预估行高的效果:
没有设置预估行高

这是设置了预估行高的效果:
设置了预估行高

新手上路,请多包涵
   [UIView performWithoutAnimation:^{
 [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
                }];
                
 我用的这个可以呀,没有闪烁问题
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题