![](/img/trans.png)
[英]Swift 3: CollectionView didSelectItemAt get selected cell label
[英]Swift UICollectionViewCell didSelectItemAt not printing label name on cell
我已經瀏覽過Google和堆棧溢出,但找不到答案。 我有一個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)
}
您需要像這樣在indexPath上單擊的單元格:
let cell = collectionView.cellForItem(at:indexPath) as! closetCollectionViewCell
然后只需從單元格變量中獲取值並打印即可。
使用此選項在didselectItem中打印所選項目
print(shirtStyle[indexPath.item])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.