第一種情況:tableview 高度使用UITableViewAutomaticDimension自適應(yīng)布局必怜,
收縮時(shí)伊群,頁面滑動(dòng)
解決:收縮先刷新,后滾動(dòng)到section
[self.mTableView reloadSections:[NSIndexSet indexSetWithIndex:section]withRowAnimation:UITableViewRowAnimationFade];
[self.mTableView scrollToRow:NSNotFound inSection:section atScrollPosition:UITableViewScrollPositionNone animated:NO];
說明:NSNotFound是收縮時(shí)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];//有動(dòng)畫的刷新
}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滑動(dòng)(防止reload滑動(dòng))
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;