在函数中访问collectionViewCell

问题描述 投票:1回答:1

我对编程很陌生,有一个问题。当用户点击按钮时,我想更改UICollectionView中的元素,但是在访问函数中的单元格时遇到麻烦。如何在没有错误的情况下访问函数中的单元格

表达式类型不明确,没有更多上下文

func resetDice(_ fourthFlippedDieIndex:IndexPath) {
    //Get the cells for the four cells that were revealed
    let dieOneCell = collectionView(diceCollectionView, cellForItemAt: firstFlippedDieIndex!) as? DiceCollectionViewCell
    let dieTwoCell = collectionView(diceCollectionView, cellForItemAt: secondFlippedDieIndex!) as? DiceCollectionViewCell
    let dieThreeCell = collectionView(diceCollectionView, cellForItemAt: thirdFlippedDieIndex!) as? DiceCollectionViewCell
    let dieFourCell = collectionView(diceCollectionView, cellForItemAt: fourthFlippedDieIndex) as? DiceCollectionViewCell

    let dieOneTurnOne = dieArray[firstFlippedDieIndex!.row]
    let dieOneTurnTwo = dieArray[firstFlippedDieIndex!.row + 4]
    let dieTwoTurnOne = dieArray[secondFlippedDieIndex!.row]
    let dieTwoTurnTwo = dieArray[secondFlippedDieIndex!.row + 4]
    let dieThreeTurnOne = dieArray[thirdFlippedDieIndex!.row]
    let dieThreeTurnTwo = dieArray[thirdFlippedDieIndex!.row + 4]
    let dieFourTurnOne = dieArray[fourthFlippedDieIndex!.row]
    let dieFourTurnTwo = dieArray[fourthFlippedDieIndex!.row + 4]

    dieOneCell!.setDie(dieOneTurnTwo)
    dieTwoCell!.setDie(dieTwoTurnTwo)
    dieThreeCell!.setDie(dieThreeTurnTwo)
    dieFourCell!.setDie(dieFourTurnTwo)

    firstFlippedDieIndex = nil
    secondFlippedDieIndex = nil
    thirdFlippedDieIndex = nil
}

我不认为我现在正在做什么,甚至无法访问该单元。let dieOneCell = collectionView.cellForItem(atIndexpath: firstFlippedDieIndex)但它导致“表达式类型在没有更多上下文的情况下是模棱两可的”错误。有人知道如何解决此问题吗?

ios swift uicollectionview
1个回答
-1
投票

因此,如果您想在单击按钮时更改collectioView,那么如果您先<重新加载 collectionView可能会更好。dataSource = [1,2,3....] func buttonAction(){ dataSource = [3,6,2...] collectionView.reloadData() }

© www.soinside.com 2019 - 2024. All rights reserved.