尤其是在UITableViewCell中放入UITextView輸入文字動(dòng)態(tài)刷新Cell高度的時(shí)候,會(huì)跳動(dòng)的問題, 以及在使用reloadSections去刷新tableView的某個(gè)section的時(shí)候
不同系統(tǒng)下可能表現(xiàn)的不一樣, 尤其我實(shí)在iPad 11.4的發(fā)現(xiàn)的問題, 而iPhone的11.4就沒問題. 很奇怪 , 所有嘗試了各種方法去解決
下面是嘗試過的方法...
// 1
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
// 2
[UIView performWithoutAnimation:^{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
}];
// 3
[self.tableView performBatchUpdates:^{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
} completion:^(BOOL finished) {}];
// 4
[self.tableView beginUpdates];
[self.tableView performBatchUpdates:^{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
} completion:^(BOOL finished) {}];
[self.tableView endUpdates];
然后在網(wǎng)上看到了這個(gè)方法:
iOS11默認(rèn)開啟Self-Sizing 如果你沒用到預(yù)估高度 那么你嘗試在Appdelegate.m中的didFinishLaunchingWithOptions方法中,加上如下代碼,看看是否有效
也可以放在UITableView的初始化或者懶加載中
if (@available(iOS 11.0, *)) {
UITableView.appearance.estimatedRowHeight = 0;
UITableView.appearance.estimatedSectionFooterHeight = 0;
UITableView.appearance.estimatedSectionHeaderHeight = 0;
}
因?yàn)?code>tableview的load和reload,是先根據(jù)預(yù)估行高做一個(gè)輪廓的搭建,再把自定義的數(shù)據(jù)填充進(jìn)去做高度的微調(diào)。所以假如不做預(yù)先的設(shè)置,默認(rèn)是根據(jù)UITableViewAutomaticDimension做預(yù)估行高的(好像是44),這樣的渲染導(dǎo)致了界面抖動(dòng),甚至到時(shí)scrollView上移或下移。
因此需要在init方法中設(shè)置 預(yù)估行高,
并盡量確保 預(yù)估行高 和heightForRowAtIndexPath、heightForHeaderInSection 、heightForFooterInSection中返回值保持一致。
這樣做的好處是去除渲染時(shí)的界面抖動(dòng),同時(shí)提高界面渲染的性能。