这是崩溃的代码

 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

有人可以找到解决方案吗

最佳答案

您没有将此选择器直接发送到实例,而是collectionview发送了它,因为它想更新单元格。

首先,确保存在indexPath,并且由于要重新加载大量单元格,因此可以创建一个包含indexPath的Array,然后立即重新加载所有元素,还可以将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")
}

10-05 21:00
查看更多