iOS UICollectionView reloadItemsAtIndexPaths: 閃退問題修復
"
錯誤原因:
列表數據section 和 indexPath.row 組數或個數在動態變化過程中執行局部刷新方法閃退,局部刷新只適合列表數據穩定情況下
第一版改錯:
我加了@try @catch, 認為當 try 局部刷新 失敗時候 catch 直接 [self.collectionView reloadData]做整體刷新好了
情況是確實避免了閃退,但是 整個頁面大部分空白了,基本沒有了視圖層級,頁面可滾,代理方法也不執行了。因此該方案不可行!!!
分析:reloadItemsAtIndexPaths 刷新本質依賴于當前已存在要更新的目標cell , 如果沒有情況,就會閃退 . 如果存在, 在tableView/collectionView的內部是先刪除該cell,再重新創建一個cell.
真正的解決方案:
(1)獲取目標indexPath
(2)預判定是否存在目標cell
(3)存在cell再執行
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; if (cell) {//這里使用局部刷新前判斷是否存在要更新的cell [[self.collectionView reloadItemsAtIndexPaths:@[indexPath]]; } else {//沒有cell如有必要刷新可使用不依賴已存在cell 和數據動態變化影響 的整體刷新方案 [self.collectionView reloadData]; }
參考:
https://www.jianshu.com/p/7663bba767c9
https://blog.csdn.net/m0_46479005/article/details/109464408
https://blog.csdn.net/XieYupeng520/article/details/51767469
https://stackoverflow.com/questions/10844306/crash-on-reloadrowsatindexpath-but-not-on-reloaddata
https://segmentfault.com/q/1010000000245170?bd_source_light=4746641
posted on 2022-09-27 11:17 ACM_Someone like you 閱讀(1258) 評論(0) 收藏 舉報
浙公網安備 33010602011771號