UICollectionView 接收到的单元格的布局属性,其索引路径不存在 [英] UICollectionView received layout attributes for a cell with an index path that does not exist

查看:25
本文介绍了UICollectionView 接收到的单元格的布局属性,其索引路径不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了两个集合视图,名称分别为 cltnEditscltnTools,我默认显示 cltnTools.当用户单击 cltnTools 的任何项目时,会出现 cltnEdits 集合视图,否则它将被隐藏.有一个带有 cltnEdits 的取消按钮,每当用户点击该取消按钮时,cltnEdits 就会消失(隐藏)并出现 cltnTools.

I have used two collection views which name is cltnEdits and cltnTools, i am Displaying cltnTools by default. when user click on any item of cltnTools then cltnEdits collection view is appeared otherwise it will be hidden. there is one cancel button with cltnEdits, whenever user click on that cancel button cltnEdits is disappeared(Hidden) and cltnTools is appeared.

我的两个集合视图都在 UIStackView 中

从集合视图中选择名称为 cltnTools 的元素时出现以下错误.

I am getting following error while selecting element from collection view which name is cltnTools.

我尝试了以下代码来解决此错误,但对我不起作用

I have Tried following code to resolve this error but its not works for me

            cltnEdits.reloadData()
            cltnEdits.collectionViewLayout.invalidateLayout()
            cltnEdits.layoutSubviews()

            cltnTools.reloadData()
            cltnTools.collectionViewLayout.invalidateLayout()
            cltnTools.layoutSubviews()

也试过这个

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    cltnEdits.collectionViewLayout.invalidateLayout()
}

并添加了这个 UICollectionViewDelegateFlowLayout 委托

func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
    return true
}

以下是我用于 cltnTools

   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if collectionView == cltnTools {

        selectedItem.push(item: "CltnTools")

        switch indexPath.row {

        case 0:

            //Edits

            arrImage = arrEditToolsImage
            arrText = arrEditToolsText

            selectedItem.push(item: arrMainToolsText[indexPath.row])

            viewCrop.image = imgImage.image

            cltnEdits.reloadData()
            cltnEdits.collectionViewLayout.invalidateLayout()
            cltnEdits.layoutSubviews()

            toolsToEdits()

            break

        case 1:

            //Effect

            selectedItem.push(item: arrMainToolsText[indexPath.row])

            arrImage = arrEffectToolsImage
            arrText = arrEffectToolsText

            cltnEdits.reloadData()
            cltnEdits.collectionViewLayout.invalidateLayout()
            cltnEdits.layoutSubviews()

            toolsToEdits()

            break

        case 2:

            //Colors

            selectedItem.push(item: arrMainToolsText[indexPath.row])

            arrImage = arrColorToolsImage

            cltnEdits.reloadData()
            cltnEdits.collectionViewLayout.invalidateLayout()
            cltnEdits.layoutSubviews()

            toolsToEdits()

            break

        case 3:

            //Text

            selectedItem.push(item: arrMainToolsText[indexPath.row])

            arrImage = arrTextToolsImage
            arrText = arrTextToolsText

            imgImage.addLabel()

            //Modify the Label
            imgImage.textColor = UIColor.black
            imgImage.textAlpha = 1
            imgImage.currentlyEditingLabel.closeView!.image = #imageLiteral(resourceName: "Delete")
            imgImage.currentlyEditingLabel.rotateView?.image = #imageLiteral(resourceName: "Resize")
            imgImage.currentlyEditingLabel.border?.strokeColor = UIColor.black.cgColor

            cltnEdits.reloadData()
            cltnEdits.collectionViewLayout.invalidateLayout()
            cltnEdits.layoutSubviews()

            toolsToEdits()


            break

        case 4:

            //Frame

            selectedItem.push(item: arrMainToolsText[indexPath.row])

            print("Frame \(arrFrameToolsImage.count)")

            arrImage = arrFrameToolsImage

            cltnEdits.reloadData()
            cltnEdits.collectionViewLayout.invalidateLayout()
            cltnEdits.layoutSubviews()

            toolsToEdits()

            break

        case 5:

            //Goodies

            selectedItem.push(item: arrMainToolsText[indexPath.row])

            arrImage = arrGoodiesToolsImage

            print("Goodies \(arrGoodiesToolsImage.count)")

            cltnEdits.reloadData()
            cltnEdits.collectionViewLayout.invalidateLayout()
            cltnEdits.layoutSubviews()

            toolsToEdits()

            break

        default:
            break

        }

    }

我该如何解决我的错误?有人请帮忙!

How can i resolve my error? someone please help!

注意:在将其标记为重复之前,请检查我的代码,我已尝试在堆栈中提出的其他问题中给出的所有解决方案.

NOTE: Before marking it duplicate please check my code that i have tried all solution given in other question asked on stack.

推荐答案

尝试交换 reloadData()invalidateLayout(),另外不要调用 layoutSubviews 直接(参见 docs)

Try to swap reloadData() and invalidateLayout(), and additionally do not call layoutSubviews directly (see docs)

cltnEdits.collectionViewLayout.invalidateLayout()
cltnEdits.reloadData()
cltnEdits.setNeedsLayout()
cltnEdits.layoutIfNeeded()


cltnTools.collectionViewLayout.invalidateLayout()
cltnTools.reloadData()
cltnTools.setNeedsLayout()
cltnTools.layoutIfNeeded()

这篇关于UICollectionView 接收到的单元格的布局属性,其索引路径不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆