Reputation: 5
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
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:
In Storyboard, set the class of your UICollectionViewCell
s as your custom subclass (in this example ItemCell
).
The key thing here is to give it an identifier, so it can be dequeued
:
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:
Upvotes: 0