gpt4 book ai didi

ios - 存储 UICollectionViewCell 状态

转载 作者:行者123 更新时间:2023-11-29 05:54:12 26 4
gpt4 key购买 nike

如何存储 UICollectionViewCell 的状态?最好使用它的indexPath。

这是一些背景知识和示例。

我目前有一个 10x10 网格的 collectionView 单元格。单击某个单元格时,它会使用该单元格的索引路径递归检查周围单元格的特定条件。

问题在于,有时周围的单元格会包含原始单元格(它使用其索引路径来检查周围单元格的单元格),从而导致无限循环。这是一个例子:

选择 IndexPath [0, 0] 处的单元格,显示 [0, 0] 处的单元格(我想存储此事实),以及周围的单元格单元格为 [[0, 1], [1, 1], [1, 0]]。

然后,递归方法以与选定单元格相同的方式检查周围的单元格,[0, 0] - 从[0, 1]开始。 indexPath [0, 1] 处的单元格已显示,现在它将用于检查下一组周围的单元格。

[0,1] 周围的单元格为 [[0, 0], [1, 0], [0, 2], [1, 2], [1, 1 ]] - 这就是问题开始/无限循环产生的地方。

存储已显示单元格状态的最佳方法是什么?检查单元格是否已选定不是一个选项,因为正如您在 [0,1] 中看到的那样,可以检查尚未选择的单元格。

我应该提到这是一个自定义单元格 - 在该自定义单元格中创建 Bool 是处理它的最佳方法吗?对于包含 100 个单元格的网格,我觉得这可能不是最好的解决方案。

编辑:

这会检查周围的单元格,然后进行递归调用。当在 didSelect 方法中选择一个单元格时,首先调用 recursiveCheck。

//this checks for the nearby positions using the cell's indexpath that is being retrieved at didSelectItemAt IndexPath.
// all the variables like nearbyForLeftEdge are the different arrays that are used to know which surrounding areas to check since differnt parts of the grid have a different positions to check
func nearbyPositionsCheck(userLocation: IndexPath) -> [IndexPath] {
var nearbyLocation = [IndexPath]()

if edgeCases(userIndexPath: userLocation) != true && cornerCases(userIndexPath: userLocation) != true {
nearbyLocation = idxPathsForEdges(idxPaths: nearbyCellsCoordinates, userLocation: userLocation)
} else if edgeCases(userIndexPath: userLocation) == true && cornerCases(userIndexPath: userLocation) == false {

if userLocation.row == 0 {
nearbyLocation = idxPathsForEdges(idxPaths: nearbyForLeftEdge, userLocation: userLocation)
} else if userLocation.section == 0 {
nearbyLocation = idxPathsForEdges(idxPaths: nearbyForTopEdge, userLocation: userLocation)
} else if userLocation.row == 9 {
nearbyLocation = idxPathsForEdges(idxPaths: nearbyForRightEdge, userLocation: userLocation)
} else if userLocation.section == 9 {
nearbyLocation = idxPathsForEdges(idxPaths: nearbyForBottomEdge, userLocation: userLocation)
}

} else if cornerCases(userIndexPath: userLocation) == true {

if userLocation == [0,0] {
nearbyLocation = idxPathsForEdges(idxPaths: nearbyTopLeft, userLocation: userLocation)
} else if userLocation == [0,7] {
nearbyLocation = idxPathsForEdges(idxPaths: nearbyForBottomLeft, userLocation: userLocation)
} else if userLocation == [7,7] {
nearbyLocation = idxPathsForEdges(idxPaths: nearbyForBottomRight, userLocation: userLocation)
} else if userLocation == [7,0] {
nearbyLocation = idxPathsForEdges(idxPaths: nearbyForTopRight, userLocation: userLocation)
}

}
return nearbyLocation
}

func recursiveCheck(userLocation: IndexPath, cell: UICollectionView) {
var newLocation = userLocation
let nearbyCell = cell.cellForItem(at: newLocation) as? CustomCollectionViewCell
let nearbyPositions = nearbyPositionsCheck(userLocation: userLocation)


for nearby in nearbyPositions {
if cellNumber(nearbyCells: nearbyPositions) > 0 { // cell number returns the number of surrounding cells nearby that meet the condition to stop checking for cells, which is != 0
nearbyCell?.label.text = String(cellNumber(nearbyCells: nearbyPositions))
nearbyCell?.backgroundColor = .green
} else if cellNumber(nearbyCells: nearbyPositions) == 0 && nearbyCell?.isRevealed == false { //if the cell # does equal zero, call this function again until it does not.
// in both situations I want to reveal the cell
nearbyCell?.label.text = String(cellNumber(nearbyCells: nearbyPositions))
nearbyCell?.backgroundColor = .cyan
// I want to check the state of the cell before calling this. If I've already A) Selected the cell this shouldn't be checked or B)The cell has already been checked via recursion or otherwise, this shouldn't be checked
recursiveCheck(userLocation: nearby, cell: cell)
}
newLocation = nearby
}
} ````

最佳答案

当你进入递归方法时,你可以将indexPath存储在NSArray中。

该方法首先需要检查给定的indexPath是否已经在该数组中。如果是,则该方法存在,因为单元格已被处理,如果否,则将 indexPath 添加到数组并继续。

关于ios - 存储 UICollectionViewCell 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55323677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com