There are advantages to designing a cell (in a table view or collection view) inside the table/collection, within a storyboard: You get to see the design in context, and you don't have to register the cell in code.
However, it breaks down a bit if you use the same cell in different tables / collection views.
As far as I can see, you have to have copies of the cell design in each table view. This is not very DRY.
I could always use registerNib with a separate xib for the cell, but this removes it from the storyboard.
What is the best practice for doing this? Is there a way to have a reference to a separate cell xib in the storyboard so I can see in in context?
You can do this with storyboard too. Set the custom class for UITableViewCell. You can use the same for multiple tableview.
Related
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.
I have 2 viewcontrollers on my storyboard, which both have a tableview. Now I would like to implement the same cells in both tableviews. Is there a way to easily reuse these cells across multiple viewcontrollers?
Yes you can use it by using custom table view cell
Example for custom table view cell
You should reuse full UITableView for this case. There is no open API for sharing cells between multiple UITableViews, but you still can reuse UITableView in case there is no moment when two controllers are shown simultaneously.
Yes, It's possible you need to create .nib file with subclassing UITableviewCell
take a look on https://medium.com/#musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722#.u8xounci2
you get to know , how to create & use .nib file.
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.
I have a UITableViewCell set up in a Storyboard UITableView that I want to also use in a different UITableView.
My current solution is to have a separate nib for the cell and load the bundle in cellForRowAtIndexPath: for both view controllers. However, this is messy and I lose a benefit of Storyboards: being able to view the whole layout on the screen at the same time.
The other solution is to copy and paste the cell. But now I have two copies of the same cell I'd need to manage.
Long shot, is there a way to dequeue a cell from a different UITableView?
I don't think you should do that. I tried once to share a cell between my main table view and a search results table view, and it caused crashes. I don't think having to copy and paste the cell a few times if you end up changing it is too much work if you really want to see everything in your storyboard. If seeing the cell isn't that important (as opposed to seeing the controller hierarchy and flow), then I would go with the xib option.
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.