我正在为游戏设置collectionView。在每个单元格中都有一个炸弹图像,星状图像或空白。游戏开始时,所有单元均关闭,看不到图像,只有绿色背景。 点击第一行中的任何单元格时,它将打开并显示带有粉红色背景的相关图像,绿色背景会按预期消失。轻按其他行中的任何一个单元格在第一行中打开一个单元格之后,新的单元格就不应打开。 问题是:其他行中新近打开的单元格打开并显示与第一行中打开的图像相同的图像。在第一个打开的单元格中,如果有炸弹或星形图像,则会出现问题,但是,如果单元格为空(打开后仅显示粉红色背景),那么我看不到此问题。
我正在使用Xcode-10.3和swift-5。我在Xcode的虚拟设备集中尝试了不同的iPhone型号,问题仍然存在。我已经清理了程序(shift + command + K),问题仍然存在。 当我在开头轻按第一行以外的行时,它们没有按预期打开。 当意外的单元格打开时,我再次轻按,它消失了。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
openCellTapped(at: indexPath)
}
// open the cell in a given in an indexPath
private func openCellTapped(at indexPath: IndexPath) {
// Find the cell in the given indexPath
let cell = collectionViewGame.cellForItem(at: indexPath) as! CollectionViewCell
// Find the item in the given indexPath
let item = gameManager.getCellDataAt(indexPath: indexPath)
// find the tapped cell's row number (first row is the lowest, however first
// indexpath.section is most upper)
let tappedCellRowNumber = numOfRows - indexPath.section - 1
// if the tapped cell is already open return
guard item.getItemCurrentOpenState() == false else { return }
// At the beginning of game only cells in first row can be opened
// if the tappedCell is in the first row
if tappedCellRowNumber == 0 {
// check all the cells in the first row. If all the cells are closed then
// open any tapped cell
for col in 0...numOfCols - 1 {
let tempIndexPath = IndexPath(item: col, section: indexPath.section)
let tempItem = gameManager.getCellDataAt(indexPath: tempIndexPath)
guard tempItem.getItemCurrentOpenState() == false else { return }
if col == numOfCols - 1 { item.openItem() }
}
// if in the first row there are some open cells, check if any neighbour of tapped cell is open
// if any neighbour is open then open the item
// if any neighbour is not open then dont open
// if the tapped cell is not in the first row
} else {
// check if any neighbour of tapped cell is open
// if there is at least one open neighbour then open the cell
// if no neighbour is open then dont open the item.
}
// update items in the cell
cell.updateItemInCell(item: item)
// Reload items in collectionView after pressing
collectionViewGame.reloadItems(at: [indexPath])
}
enum itemState { case Bomb, Star, Empty }
var itemCurrentState: itemState = .Empty
var isItemOpen = false
func getItemCurrentOpenState() -> Bool {
return isItemOpen
}
func openItem() {
isItemOpen = true
}
func getItemCurrentState() -> itemState {
return itemCurrentState
}
func updateItemInCell(item: ItemInCell) {
let itemState = item.getItemCurrentState()
// set the background color of cell according to item's open state
// if item is open
if item.getItemCurrentOpenState() {
// pink
imageViewCell.backgroundColor = UIColor(red: 0.6, green: 0.2, blue: 0.5, alpha: 0.6)
// For each state of item define the image
switch itemState {
case .Bomb:
imageViewCell.image = UIImage(named: "bomb.png")
case .Star:
imageViewCell.image = UIImage(named: "rewarding_star")
case .Empty:
break
}
// if item is closed
} else {
// green
imageViewCell.backgroundColor = UIColor(red: 0.2, green: 0.5, blue: 0.35, alpha: 0.6)
}
程序未提供任何错误消息。为了打开单元格,getItemCurrentOpenState()值应为true。我检查了一下,即使它的值再次为false,单元格也会打开。