Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a reusable cell for both TableView and CollectionView

Tags:

ios

xib

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?

like image 288
shippo7 Avatar asked Jan 20 '17 17:01

shippo7


1 Answers

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

like image 134
Gokul G Avatar answered Oct 06 '22 02:10

Gokul G