iOS 解決tableview 折疊 reload 頁面向上滑動一段距離問題

第一種情況:tableview 高度使用UITableViewAutomaticDimension自適應(yīng)布局,
收縮時,頁面滑動
解決:收縮先刷新,后滾動到section

    [self.mTableView reloadSections:[NSIndexSet indexSetWithIndex:section]withRowAnimation:UITableViewRowAnimationFade];
    [self.mTableView scrollToRow:NSNotFound inSection:section atScrollPosition:UITableViewScrollPositionNone animated:NO];
說明:NSNotFound是收縮時row為空,若直接設(shè)置為0,會導(dǎo)致越界崩潰

示例代碼

//存儲展開或收起狀態(tài)
-(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{//反之關(guān)閉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自適應(yīng)布局
解決:嘗試把預(yù)測高度設(shè)置為0
// 添加數(shù)據(jù)刷新后,防止tableview滑動(防止reload滑動)

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容