Subclassing existing custom UITableViewCell + XIB - ios

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).

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

How to access UITableViewCell from UITableView in different UIViewController?

I have very complex cell in UITableView within UIViewControllerA. Now I need to use the same cell within UITableView of UIViewControllerB.
How to do this without copy and paste views from one scene to another?
Do not wanna use xib approach.
Is it related to registerNib:forCellReuseIdentifier?
You mentioned you don't want to use xib. Storyboards don't support this and therefore the last option you have is to construct your UITableViewCell in code. With the new Auto Layout anchors, it's not that bad.

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.

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.

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