简体   繁体   中英

Swift UICollectionViewCell didSelectItemAt not printing label name on cell

I have looked through google and stack overflow and I was not able to find an answer. I have a UICollectionView and would like to take the user to another view upon the cell being clicked. But before doing so I would like to click the cell on the simulator and have the label's name printed in the console so I can from there figure out how to write the performSegue method. I am having issues with the didSelectItemAt function.

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)
}

You need to get the cell that was clicked at indexPath like this:

let cell = collectionView.cellForItem(at:indexPath) as! closetCollectionViewCell

Then just get the values from the cell variables and print.

使用此选项在didselectItem中打印所选项目

print(shirtStyle[indexPath.item])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM