So I have a few collection views that use the same cell (which basically looks like table view but for my purposes, I need to use the collection view). In the newest feature I am using tableView and cell for it looks the same, so is there a way to use the same cell for that view too?
2 Answers
I am guessing it's a little bit late for an answer, but better late than never, right?
In contrast to Timmy's answer, and following Amal's and Gokul's answer, I was able to build a reusable cell for both UITableView
and UICollectionView
with the help of XIB
files. Unfortunately, due to UIKit's class inheritance, we cannot make our custom cells inherit from both UITableViewCell
and UICollectionViewCell
, so the key thing is to create your XIB
and use it inside a cell as a Custom View. This way, you're able to reuse your view for both scenarios.
In my case, I needed to code the Apple Music app using Interface Builder/Storyboards. Some components were reused by other views, so I decided to create a XIB for them. These were the steps that helped me create a reusable view for both a UITableView
and a UICollectionView
:
- Start by designing your
XIB
, whether by Interface Builder/Storyboards or programmatically. There are a lot of tutorials out there. Specifically, I used this one. - With your
XIB
ready, place it inside yourTableViewCell
orCollectionViewCell
. Don't forget to set the custom class of your view to match yourXIB
, otherwise this won't work. - With your cell ready, what's left now is to
dequeue
it in yourcellForRow at
orcellForItem at
in your tableView/collectionView data source method and customize it as you wish.
Hope this helps :D

- 11
- 4
Simply, no. A TableViewCell is of type UITableViewCell
which is very different from UICollectionViewCell
.

- 4,098
- 2
- 14
- 34