2015-10-19 92 views
0

這是它在哪裏崩潰CollectionView.ReloadItemsForIndexPaths崩潰迅速

for var i=0;i<Data.NearByList.count;i++ { 

      if let collectionView = collview { 
       collectionView.reloadItemsAtIndexPaths([NSIndexPath(forItem: i, inSection: 0)]) 
      } 

     } 

我想在viewdidApear重裝的CollectionView數據,因爲一些圖像顯示不正確 但總是收到此錯誤

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSStackBlock__ _setNeedsFocusItemOverlayUpdate]: unrecognized selector sent to instance 0x45426c' *** First throw call stack: (0x2799386b 0x39092dff 0x27999035 0x27996c8f 0x278c62b8 0x2c2387d3 0x2c238379 0x2bb09dc3 0x2c241e7b 0x2bc82b45 0x2c2435fd 0x2bab3abd 0x2c242e25 0x2c2413fd 0x2bc7ea1d 0x1efe9c 0x2029e4 0x28694b8b 0x27947ffd 0x27947a0b 0x279477e1 0x2799bac3 0x278a904b 0x28692317 0x28696e4b 0x1fe0ec 0x117a6c 0xfe684 0x117bfc 0x27218a25 0x28750a05 0x286b24af 0x286a48bf 0x28752cc5 0x14bbdab 0x14c0829 0x27956595 0x27954a8f 0x278a71e9 0x278a6fdd 0x30b4baf9 0x2bb0c18d 0x1f51a8 0x397bd873) libc++abi.dylib: terminating with uncaught exception of type NSException

代碼

有人可以找到這個解決方案

+0

爲什麼不只是調用collectionView.reloadData() ?爲什麼這個迭代需要? – dirtydanee

+0

它重新加載了一些單元格,並且一些圖像仍然不正確 –

+0

比您的數據源未刷新,我認爲。你應該照顧,而不是黑客collectionView, – dirtydanee

回答

1

你是n將此選擇器直接發送到實例,但collectionview正在發送它,因爲它想更新單元。

首先,確保indexPath存在,並且由於您正在重新載入很多單元格,您可以創建一個包含Array的indexPaths,然後一次全部重新加載,還可以將reload-call包裝爲performBatchUpdates調用,如下所示。

var indexPathsToReload = [NSIndexPath]() 
for var i in 0..<Data.NearByList.count { 
    indexPathsToReload.append(NSIndexPath(forItem: i, inSection: 0)) 
} 

,那麼你可以簡單地重新加載一次全部

collectionView.reloadItemsAtIndexPaths(indexPathsToReload) 

或分批更新(可以包括其他更新太)

collectionView.performBatchUpdates({() -> Void in 
    collectionView.reloadItemsAtIndexPaths(indexPathsToReload) 
    // Any additional change 
    }) { (_) -> Void in 
     print("Finished") 
} 
+0

感謝它的作品 –

+0

嘿它不適用於設備,我不明白why.Still給我這個錯誤 –

+0

確保您正在重新加載它,並且您正在從主線程中執行此操作,同時請確保單元格已正確設置。 – ogres