0

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?

Andreyka
  • 69
  • 6

2 Answers2

1

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:

  1. 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.
  2. With your XIB ready, place it inside your TableViewCell or CollectionViewCell. Don't forget to set the custom class of your view to match your XIB, otherwise this won't work.
  3. With your cell ready, what's left now is to dequeue it in your cellForRow at or cellForItem at in your tableView/collectionView data source method and customize it as you wish.

Hope this helps :D

0

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

Timmy
  • 4,098
  • 2
  • 14
  • 34