iOS 解决tableview 折叠 reload 页面向上滑

2021-10-24  本文已影响0人  橙_知足常乐

第一种情况: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;

上一篇 下一篇

猜你喜欢

热点阅读