Gabriel Sória
Gabriel Sória

Reputation: 5

How to load an image from a function in another class in UICollectionView? I'm using paint code

I have a collection view which will have to load the content of the cells but I don't want to use images from assets. So, I create a class with Paint Code which draws some images for me. However, I don't know how to load the images that paint code drew for me in my UICollectionView cells using dequeueReusableCell.

Upvotes: 0

Views: 61

Answers (1)

backslash-f
backslash-f

Reputation: 8193

I'm gonna usa a TableView as example, but it's the same thing with a UICollectionView...

Create a subclass of UICollectionViewCell.
In this example I'm calling it ItemCell:

class ItemCell: UITableViewCell {...} 

Create and configure a .xib file.
This file lays out your cell, and holds a UIView that represents your PaintCode drawable:

customCell

In Storyboard, set the class of your UICollectionViewCells as your custom subclass (in this example ItemCell).
The key thing here is to give it an identifier, so it can be dequeued:

configuration

Code example to reuse your custom cell:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = UITableViewCell()

    // Constants.itemCellName matches the above identifier.
    if let itemCell = tableView.dequeueReusableCell(withIdentifier: Constants.itemCellName) as? ItemCell {
        configure(itemCell, at: indexPath)
        cell = itemCell
    }

    return cell
}

Profit:

example

Upvotes: 0

Related Questions