簡體   English   中英

如何獲取UICollectionViewCell的indexPath並保存

[英]How can I get the indexPath of a UICollectionViewCell and save it

我有一個UICollectionView ,其中有48個正方形。 我希望代碼知道用戶選擇了哪個UICollectionViewCell 用戶選擇了UICollectionViewCell之后,用戶必須按下該VC的顏色。

如何獲得所選的單元格?

我這樣做:

func selectedCell (at indexPath: IndexPath) {
    let indexPathForFirstRow = indexPath
    gamePad.selectItem(at: indexPathForFirstRow, animated: false, scrollPosition: UICollectionView.ScrollPosition(rawValue: 0))
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell: UICollectionViewCell = gamePad.cellForItem(at: indexPath)!

    switch cell.backgroundColor {
    case UIColor.lightGray:
        cell.backgroundColor = UIColor.gray
    case UIColor.gray:
        cell.backgroundColor = UIColor.lightGray
    default:
        break
    }
}

試試這個代碼

func selectedCell(forCell cell: UITableViewCell) -> IndexPath? {
        let cellPosition: CGPoint = cell.convert(CGPoint.zero, to: self)
        return self.indexPathForRow(at: cellPosition)
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM