iOS 解决tableview 折叠 reload 页面向上滑动一段距离问题

第一种情况:tableview 高度使用UITableViewAutomaticDimension自适应布局,
收缩时,页面滑动
解决:收缩先刷新,后滚动到section

    [self.mTableView reloadSections:[NSIndexSet indexSetWithIndex:section]withRowAnimation:UITableViewRowAnimationFade];
    [self.mTableView scrollToRow:NSNotFound inSection:section atScrollPosition:UITableViewScrollPositionNone animated:NO];
说明:NSNotFound是收缩时row为空,若直接设置为0,会导致越界崩溃

示例代码

//存储展开或收起状态
-(void)setSaveIsOpenStatus:(NSInteger)section{
    
    if ([self.isOpenDic[@(section)] integerValue] == 0) {//如果是0,就把1赋给字典,打开cell
        //展开
        [self.isOpenDic setObject:@"1" forKey:@(section)];
       
        [self.mTableView reloadSections:[NSIndexSet indexSetWithIndex:section]withRowAnimation:UITableViewRowAnimationFade];//有动画的刷新

    }else{//反之关闭cell 收缩
        [self.isOpenDic setObject:@"0" forKey:@(section)];
        [self.mTableView reloadSections:[NSIndexSet indexSetWithIndex:section]withRowAnimation:UITableViewRowAnimationFade];
        [self.mTableView scrollToRow:NSNotFound inSection:section atScrollPosition:UITableViewScrollPositionNone animated:NO];
    } 
}

第二种情况,tableView 并没有使用UITableViewAutomaticDimension自适应布局
解决:尝试把预测高度设置为0
// 添加数据刷新后,防止tableview滑动(防止reload滑动)

    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容