没有reuseIdentifier加载固定数量的UICollectionViewCells的方法是什么? (迅速)

时间:2018-03-28 12:56:27

标签: ios swift uicollectionview uicollectionviewcell reuseidentifier

我在UICollectionView(水平滚动)中有5列,一切正常。

每个UICollectionViewCell内部都包含自己的UITableView,有时会有很多数据,因此UICollectionView 滚动不是那么顺利

我意识到主要的问题是重用单元格,因为只有当表格包含大量数据(10多行)时才会出现问题。由于数据不经常更新,并且在滚动时实际上不需要重复使用它,我相信这将解决“平滑滚动”问题。

其中一个解决方案可能是为UITableView添加分页,因此我可以保留reuseIdentifier而不考虑内存,但我也想检查这种方式。

目前,我正在使用它:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCustomIdentifier", for: indexPath) as? MyCustomCell {
        // Cell configuration code
        return cell
    }

    return UICollectionViewCell()
}

我试过这样的事情:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = UICollectionViewCell() as? MyCustomCell {
        // Cell configuration code
        return cell
    }

    return UICollectionViewCell()
}

但每次当我删除reuseIdentifier时,它都会给我这个错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier - cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:'

0 个答案:

没有答案