UICollectionView received layout attributes for a cell with an index path that does not exist

只谈情不闲聊 提交于 2019-12-24 12:29:59

问题


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.

My both Collection views are in UIStackView

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()

and also tried this

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    cltnEdits.collectionViewLayout.invalidateLayout()
}

and added this UICollectionViewDelegateFlowLayout delegate

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

following is my didSelect method for 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.


回答1:


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()


来源:https://stackoverflow.com/questions/46903770/uicollectionview-received-layout-attributes-for-a-cell-with-an-index-path-that-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!