我看了谷歌和堆栈溢出,我无法找到答案。我有一个UICollectionView,并希望在单击单元格时将用户带到另一个视图。但在此之前我想点击模拟器上的单元格并在控制台中打印标签的名称,以便我可以从中找出如何编写performSegue方法。我遇到了didSelectItemAt函数的问题。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let clothesCell = collectionView.dequeueReusableCell(withReuseIdentifier: "clothesCell", for: indexPath) as! closetCollectionViewCell
clothesCell.clothingName.text = shirtStyle[indexPath.item]
clothesCell.clothingColor.text = shirtColor[indexPath.item]
clothesCell.clothingSize.text = "\(sizes[indexPath.item])"
return clothesCell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.item.clothingName)
}
答案 0 :(得分:1)
使用此选项在didselectItem中打印所选项目
print(shirtStyle[indexPath.item])
答案 1 :(得分:0)
您需要获取在indexPath中单击的单元格,如下所示:
let cell = collectionView.cellForItem(at:indexPath) as! closetCollectionViewCell
然后从单元格变量中获取值并打印。