Is possible to share a xib between a UITableViewCell and a UICollectionViewCell? - ios

I am looking to factorize some code and xib for a view design shared between a UITableViewCell and UICollectionViewCell. Is it possible share the same xib between a UITableViewCell and UICollectionViewCell? or Is it possible to share the same xib for a subview?.

you could do something like this by just using a UIView super view. Then each of those cells would just need to load the shared view into the cells. I think you may lose the ability for auto layout to work with UIAutomaticDimension for autosizing cells.

Related

How to make a reusable view cells for UITableViewCell and UICollectionViewCell ? I've tried to sublassing UIView for the cell but it's not working

I want to reuse my UITableViewCell to my UICollectionViewCell.
Until now, I always use 2 xibs, 1 for UITableViewCell and 1 for UICollectionViewCell and I think it's kinda waste.
I've tried to subclassing UIView for the cell but it's not working.
A way to do this is by using reusable views. Here is a Github project that can help: NibDesignable
You can also do this without using the project I mentioned, but it does make it easier. You then create a UIView with a View nib and put all your UI into this as well as any code for setting the UI components. Then you create your UITableViewCell and UICollectionViewCell and then just use the view that you created. You can also use prototype cells in a Storyboard for CollectionView or TableView if you prefer.

Universal cell for UITableView and UICollectionView

I was wondering if there are some best practices for implementing universal cell which can be used with both of UITableView and UICollectionView...
I tried to put UITableViewCell into CollectionView but Xcode complained about this. I have just one variant left: make UIView which will implement cell's view and then implement UITableViewCell and UICollectionViewCell subclasses which will wrap this custom UIView so it could be placed into Table/Colleciton view. Is that the right way to achieve my goal?
Any thoughts on this?
Your idea can be implemented if the UIView's subviews are similar.You can make a template of UIView,create multiple widgets inside it,and create instance in UITableViewCell and UICollectionViewCell,as UITableViewCell and UICollectionViewCell are usually reused, you need to separate the data model and views,so realize your idea.

iOS alternative for Androids <include> tag?

How am I supposed to reuse views in iOS?
For example, I have a view that returns as a CollectionViewCell, inside of a UITableViewCell and on a specific spot in a UIViewController.
What is the recommended way to deal with this?
Do I make a xib and add a holder view everywhere I need it?
But, then to react to size changes, I need to add constraints in code, which seems to be too much hassle just for a simple inclusion of a xib.
Is there a feature in Xcode I can use?
Create an UICollectionViewCell in a XIB. Then this cell is placed inside an UICollectionView. You can place this UICollectionView basically everywhere, also inside an UITableViewCell. As the UICollectionView can be auto-layout constraint in the Storyboard the size changes automatically. No need for programmatic constraints. When you want to reuse the UICollectionViewCell just put it inside another UICollectionView in another UIViewController or UITableViewCell.
Btw: to use this just as "reusing"-feature you can disable the scrolling in the UICollectionView.

Use the same cell type in different screens in storyboard without duplication

I have a UITableViewController using many different types of cells.
In the code, each of those cells has its own UITableViewCell subclass.
In storyboard, they are all in the same screen.
Now I need to use the same cells in other screens as well.
How do I do that without having to copy and paste my cell in storyboard in the new screens?
I don't want any duplication in storyboard when it comes to layout and constraints.
Your best option is probably to move the cells into separate XIB files. You need to use the registerNib: method on UITableView to tell the table where it can find the cell definition.
You can create a
UITableViewCell
class.
Use this class as the cell's custom class and Ctrl link your cell's items to the property in this class.
Then you can use this cell class for other table views

Adding views to tableViewCell contentView on xib?

I have created a xib file that is setup as a custom UITableViewCell class. This xib is simple and just has a UILabel inside of it with constraints binding it to the superview. I am utilizing this tutorial to implement dynamic cell height. Everything should be working, but by using a xib all of the views I add are not added as subviews of the content view, but the cell itself. This is creating an issue with autolayout. The dynamic resizing is dependent upon using the contentView of the cell to determine the required height.
How can I add a view to a xib that will be a subview of the cells contentView and not the cell itself?
This ended up being an overlook on my part. I had created the cell using the default view that xib's are initially setup with. I needed to drag out a UITableViewCell view from the object library and add my labels etc. to the content view of that cell.

Resources