Creating and Using Custom UITableViewCell Template in Storyboard - ios

I have a UITableView where I'd like to have multiple types of UITableViewCells. The different types of cells have a lot of similar properties, but have a few key differences, so I was hoping to create a template UITableViewCell class (TemplateCell) that the different cell types could extend.
I've tried doing this by creating a the TemplateCell class and having an associated .xib file. I then tried to go to my UITableView storyboard file and created my various cell types that subclassed my template. However, when I added those cell types to the storyboard file, they showed up as blank and didn't have any of the properties I had in the template file.
Is there an better way to create UITableViewCell templates?

You can create a TemplateCell class and separate classes for every other Cell (inherited from TemplateCell). Then you could assign common IBOutlets of EVERY subclassed cell to it TemplateCell class. Or you could operate with them by looking for the views with specific tags if you don't want to bother with outlets for base class.

Unfortunately you can't draw views in IB respecting some base view, so you should draw each cell separately, but you can use common outlets declared in the base class, and those outlets (properties) that differ, put into inherited cell classes.
Without 3d-parties IB doesn't support loading views from xib to a storyboard or another xib. You can use XXNibBridge for that.

Related

UICollectionViewCell subclass that uses its parent's xib

I have a custom UICollectionViewCell subclass, lets call it CellClassOne, and I'm trying to create a subclass of that cell, called CellClassTwo so I can change a property and modify some constraints in its view in awakeFromNib.
However, when I register my cell in my collectionView it gets loaded form the xib, so it has the parent's class, CellClassOne. How can I create CellClassTwo that can be dequeued by my collectionView and have its class set to CellClassTwo (and any properties and ovverides with that)?
I'm trying to avoid setting my properties in cellForRow since I'm trying to reuse my cell in different parts of my app but need slightly different paddings for some views, and I don't want to create duplicate Xib files for this.
If I must go with the duplicate files, then it may be better to stick with configuring the views in cellForRow.
Iā€™m not sure that you can achieve what you are looking for via xib, but a viable way that should work is creating the CellClassOne programmatically ( including all the subviews and related layout constraints) following the cell lifecycles, and then subclassing it with CellClassTwo accordingly. That way you should be able to register the cell like ā€˜ collectionView.register(CellClassTwo.self, forCellReuseIdentifier: "myCell")ā€˜.

Custom UITableViewCell - subclass UITableViewCell with xib vs using tags

In order to implement a custom UITableViewCell, I know of two options:
Create a prototype cell in the storyboard view and assign tags to the cell's constituent views
Create a subclass of UITableViewCell with an associated xib
Using the tags seems simpler. I see that it has a couple of disadvantages including the inability to re-use the custom cell in more than one scene and potentially a lot of cell specific code in the view controller which could be an issue with a bunch of different cells.
For one or two cells that do not need to be reused in multiple scenes, does anyone know of any other considerations that I should take into account that might lead me to prefer a UITableViewCell subclass?
My recommendation is to use a UITableViewCell subclass with a storyboard prototype cell. Simply link your constituent views to IBOutlet properties as you would with any other class.
If you want to use the same cell in multiple tables then you need to re layout the prototype cell in each tableView, but you can cut and paste. Using tags results in unnecessary code to get references to the UI elements.

UITableView with 8 inherited cell type

I have a UITableView, with 8 differents type of cell. Those custom cell have some design in commom (like a upper left icon, a title, a subtitle...). However, under those common features, each cell is different. In order to have less to maintain, my 8 cells inherits from a default abstract cell with the commons IBOutlet.
Now my question is : what is the best, most proper way to do this?
At first, I thought of using registerNib:forCellReuseIdentifier: but this means 8 different xib. In this case, if one of the common design feature change, I would need to go trought all 8 xibs to change the same thing. I thnik it's not very productive and clean.
I thought also of registerClass:forCellReuseIdentifier: but this methods does not create the cell with a nib, so I would need to do everything programmatically.
The solution could be have one common xib, different registered class in the tableview, and those will be responsible for using their own custom design. But I can't see how to achieve this with those two previous methods.
I would use a single nib file with 8 different UITableViewCell subclasses. You should be able to use registerClass:forCellReuseIdentifier: and inside the class (which subclasses UITableViewCell) you make use of the nib file. If it is not clear I'll try to provide some code.

Superclass with XIB and its subclass with XIB?

In my UI, there are three different table view cells. Well,not THAT different, they have some elements in common. So a base class capturing common elements would be in order.
How to apply basic OOP principles when I have the base class with XIB file?
How would I then add missing differing elements in respective subclasses if they ALSO have XIB files? (I want them to have XIB files as I am too lazy to edit frames and other properties of those elements in code..who has time for that nowadays...)
I mean how do I solve this superXIB - subXIB relationship thing?
Interesting.. But there is no way you can load multiple xibs from the bundle for a view or merge multiple xibs into one! :)
So I think you cannot have a superXIB - subXIB relationship.
You can create a base Table cell with no xib, containing some common things. Other tableview cells derive this. Have separate xib's for these subviews so that you can set frame and stuff.

Storyboards: How to add outlets to a prototype UITableViewCell?

I have added prototype cell in one of the controllers that is part of the storyboard I'm designing. It has various labels, buttons, bells and whistles.
But how do I know create outlets to that cell's elements? If I click the assistant in Xcode, it will show me the dummy source of my inherited UITableViewController and not of the "DetailsCell" which inherits from UITableViewCell and is specified in the custom class input field in IB.
I could of course use ViewWithTag() but I'd rather have something strong typed.
With any object you can create with IB, you can assign it to a different class (one that you wrote). This is not different for cells. In the same way you can make IB instantiate MyFooView instead of UIView (MyFooView being derived from UIView), you can do the same for the cells.
Click your cell, click the 3rd icon on the shelf to the right. You will see a section called "Custom Class" and a text field called Class. Select the new class you've just created that is derived from UITableViewCell (let's call it MyCell). You can add IBOutlets to the MyCell class.
You first need to create a class DetailedCell and add IBOutlets to that class.
Then in the Storyboard/IB, assign that class to the prototype cell and those outlets will be available to fill.

Resources