[英]How can I get the indexPath of a UICollectionViewCell and save it
I have a UICollectionView
, in which are 48 squares. 我有一个
UICollectionView
,其中有48个正方形。 I want that the code knows which UICollectionViewCell
is selected by the user. 我希望代码知道用户选择了哪个
UICollectionViewCell
。 After the UICollectionViewCell
is selected by the user, the user has to press a color, which are in this VC
as well. 用户选择了
UICollectionViewCell
之后,用户必须按下该VC
的颜色。
How can I get the cell which is selected? 如何获得所选的单元格?
I did this: 我这样做:
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
}
}
Try this code 试试这个代码
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.