存储UICollectionViewCell状态

时间:2019-03-24 12:13:12

标签: ios swift uicollectionviewcell

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

这里有一些背景和一个例子。

我目前有一个10x10的collectionView单元格网格。单击某个单元格时,它将使用该单元格的indexPath递归检查周围的单元格是否有特定条件。

这是问题所在,有时周围的单元格将包含原始单元格(用于检查周围单元格的indexPath所使用的单元格),从而导致无限循环。这是一个示例:

位于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
    }
} ````

1 个答案:

答案 0 :(得分:0)

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

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