第一種情況: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;