shippo7
shippo7

Reputation: 407

How to make a reusable cell for both TableView and CollectionView

I have a TableView and a CollectionView in different ViewController. Now I am showing an exactly same cell in both of them. So I want to create a reusable cell for convenience and easy maintenance.

I tried to create a xib and set the custom class to an UITableViewCell class, then I can register and load it in the UITableView. However, I cannot reuse this xib in the UICollectionView because CollectionView cannot load TableViewCell.

So my question is that is there any good way to make a reusable cell for both TableView and CollectionView?

Upvotes: 7

Views: 2785

Answers (1)

Gokul G
Gokul G

Reputation: 698

UITableViewCell <- UIView

UICollectionViewCell <- UICollectionReusableView <- UIView

both are from UIView, so i think you can create a xib for UIView, and use that view in your UITableViewCell and UICollectionViewCell. Eg: How to load a xib file in a UIView

and since both cell are going to have common code for some cases you can use category, to share the common code Eg: Sharing code between UITableViewCell and UICollectionViewCell

Upvotes: 4

Related Questions