Vaisakh KP
Vaisakh KP

Reputation: 497

Unable to access collectionView cell variables using global variable

Here i made a collectionView cell variable for accessing both objects. But unable to access the objects inside the cell variable

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell: UICollectionViewCell!
    if collectionView == collectionView1 {
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachment", for: indexPath) as! AttachmentCell
        cell.imgAttachment.image = imageArray1[indexPath.row]
        cell.delegate = self
    }
    else if collectionView == collectionView2 {
       cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachmentView", for: indexPath) as! AttachmentViewCell
       cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon
    }
    return cell

Value of type 'UICollectionViewCell?' has no member 'imgAttachment'

Upvotes: 4

Views: 536

Answers (2)

Rakesha Shastri
Rakesha Shastri

Reputation: 11242

The problem lies here.

var cell: UICollectionViewCell!

You have declared the cell to be of type UICollectionViewCell. So, no matter which subclass you store inside it, you cell will only be of type UICollectionViewCell.

You should change it like this,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView === collectionView1 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachment", for: indexPath) as! AttachmentCell
        cell.imgAttachment.image = imageArray1[indexPath.row]
        cell.delegate = self
        return cell
    } else if collectionView === collectionView2 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachmentView", for: indexPath) as! AttachmentViewCell
        cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon
        return cell
    } else {
        // Return the proper cell for other cases
    }
}

Or, if you are adamant you need only a single return statement at the end of the delegate, then you could do it like this,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var yourCell: UICollectionViewCell!
    if collectionView === collectionView1 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachment", for: indexPath) as! AttachmentCell
        cell.imgAttachment.image = imageArray1[indexPath.row]
        cell.delegate = self
        yourCell = cell
    } else if collectionView === collectionView2 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachmentView", for: indexPath) as! AttachmentViewCell
        cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon
        yourCell = cell
    } else {
        // Return the proper cell for other cases
    }
    return yourCell
}

Upvotes: 3

Kamran
Kamran

Reputation: 15258

You need to silent the compiler by casting the cell as AttachmentCell. See the below example,

(cell as! AttachmentCell).imgAttachment.image = imageArray1[indexPath.row]
(cell as! AttachmentCell).delegate = self

The reason compiler is not able to recognize the variable is the declaration of variable cell as UICollectionViewCell. As there is no imgAttachment variable in UICollectionViewCell so compiler will complain.

Upvotes: 2

Related Questions