简体   繁体   中英

How do I get parsed JSON Data into CollectionViewCell

I have what appears to be successfully parsed AS3 image links to an array of UIImage named 'smImg'.

I am now trying to append each link as a UIImage to my UICollectionViewCell's imageView (where I added an IBOutlet 'imageView'.) I keep getting the error 'UICollectionViewCell' does not have a member named 'imageView'. Relevant code below.

ViewModel.swift :

func imageForItemAtIndexPath(indexPath: NSIndexPath) -> UIImage {
     return smImg[indexPath.row]
}

ViewController.swift

func configureCell(cell: UICollectionViewCell, atIndexPath indexPath: NSIndexPath) {
    cell.imageView!.image = viewModel.imageForItemAtIndexPath(indexPath) //Error occurs here
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as PhotoCell
    configureCell(cell, atIndexPath: indexPath)
    return cell
}

PhotoCell.swift

class PhotoCell: UICollectionViewCell {


@IBOutlet weak var imageView: UIImageView!

}

Can someone please help me fix this? Any help is much appreciated, I try not to ask too many questions on here but have been unable to find a solution on my own.

Your method

func configureCell(cell: UICollectionViewCell, atIndexPath indexPath: NSIndexPath) {
    cell.imageView!.image = viewModel.imageForItemAtIndexPath(indexPath) //Error occurs here
}

is expecting a UICollectionViewCell, you should change it to expect a photo cell since that is what you are sending it.

func configureCell(cell: PhotoCell, atIndexPath indexPath: NSIndexPath) {
    cell.imageView!.image = viewModel.imageForItemAtIndexPath(indexPath) //Error occurs here
}

You also need to make sure you're telling the collectionView to use PhotoCell as its cell type. So when you initialize the collection view you need to have a line that looks something like this

collectionView.registerClass(PhotoCell.self, forCellWithReuseIdentifier: "Reuse Identifier")

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