Search code examples
iosswiftuicollectionviewcell

Accessing a CollectionViewCells data when selected


I have a UICollectionViewCell who's data I want to access when tapped. The data will be used in another view controller... For instance, when I tap on a friend, the next viewcontroller presented will be a page with the tapped friends data.

Here is my didSelectItemAt IndexPath

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let layout = UICollectionViewFlowLayout()
    let vc = JoinGroupChatController(collectionViewLayout: layout)
    vc.selectedIndex = indexPath

    let liveCell = LiveCell()
    vc.descriptionLabel = liveCell.descriptionLabel.text

    present(vc, animated: true, completion: nil)
}

In essence this is what I want to do however I need the indexPaths specific description label not the default liveCell label text.

I know I need to use the indexPath somewhere just not sure where / how. Any suggestions?


Solution

  • replace

    let liveCell = LiveCell()
    

    with

    let liveCell = collectionView.cellForItem(at: indexPath) as! LiveCell