UICollectionViewCell subclass that uses its parent's xib - ios

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")ā€˜.

Related

Subclassing existing custom UITableViewCell + XIB

I have a custom UITableViewCell called StandardTableViewCell. This is made in a XIB with autolayout.
The cell is registered with the method registerNib:forCellReuseIdentifier:
What I am trying to do now is subclass StandardTableViewCell without making a new XIB.
Is it possible to reuse the cell without making a new XIB while keeping the cell's File Owner?
Have you thought of creating a category instead of subclassing? Not sure what you're exactly doing, but that might work instead.
Unfortunately that's not possible, so you have to use a less than ideal solution, here's some options:
Implement the functionality in the same Standard cell and toggle it.
Duplicate the standard nib and replace the cell class.
Don't have a standard nib and just make one for each subclass (if they are different this is probably the best idea).

Creating and Using Custom UITableViewCell Template in Storyboard

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.

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.

How to reuse custom UITableViewCell defined as a prototyped cell in Storyboard controller

I have the only cell template for items at two different UITableViewControllers/TableViews.
What I need is to define it once and then reuse at other UITableView via
UITableView.DequeueReusableCell(CellId);
The issue is that is when I call this method on UITableView which doesn't contain cell prototype I'm getting NULL.
How to reuse my prototyped cell across multiple table controllers?
I want to define cell template in storyboard, NOT xib.
It turned out that the only way to reuse a cell it to design it with xib and register at tableview that xib with cellid.
Just copy-paste your prototype cell in every table view controller where you need it.
And if I understand your question, in a standard and proper way, it's not possible to dequeue a cell from another table view, Apple implementation handles this mechanism itself.
Using xib for a reusable cell is beneficial while cell design is fixed in the whole app. But when there are conditional requirements or slight changes in design or functionality and remaining design and functionality is same for tableview cell, in this case if you still want to reuse the code then you can subclass tableview cell class.

Add right margin for detailTextLabel in UITableViewCell

What's the easiest way to add right margin to detailTextLabel inside an instance of UITableViewCell?
The detailTextLabel used by the standard UITableViewCellLabel is not a standard label but a special subclass, private, called UITableViewLabel. Customizing it it's not easy as you probably experienced as some methods are overridden by the subclass. You can take some control by de-queing the cell from a reusable pool (because in such case the cell is not recreated and so some of the overridden methods are not called again and in such case basic UILabel settings will apply) but the result couldn't be satisfactory.
So the best approach for custom views is not to try to tweak the existing on-the-shelf views but make a custom cell. This can be done programmatically in the tableView:cellForRow:atIndexPath: delegate method, or by creating a UITableView subclass or loading it from a nib file. Even for simple changes like this.

Resources