Reputation: 407
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
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