How to reuse UITableViewCells - ios

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.

Related

I need help knowing where to start on designing a tableview or if a tableview is what I need

As in title, I don't know where to start with creating a view like this. Do I hard code it or use XCode editor? Is a table view what I need?
Yes, a UITableView is what you needed.
You need to create a TableView Controller.
Follow by creating a TableViewCell
Then you need to start coding. Hook up the delegate and data source methods.
For more information, check this tutorial by Apple:
https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/
Here you have a tableView with section header indicating the date of the matches, the tableView has a custom designed cell, each cell contains the names of the two competing team.
start by adding the tableView to your viewController using storyboards, with the delegate and datasource connected.
the sub class from UITableViewCell and create your custom cell with an xib file.

UITableView Inside a UICollectionView

How can I set multiple table view data inside a collectionView?
I think its too complex to add multiple table views inside one UICollectionViewCell. Embedded scroll views is not recommended in terms of UX.
But you can do it by subclassing The UICollectionViewCell and be the delegate/datasources of table views.
You can use only one table with multiple sections instead of multiple tables.
You can achieve this by giving tag to each UITableView in all the UICollectionViewCells and you have to manage all UITableViewDataSource and UITableViewDelegate methods by tags of particular tableView.
I know its complicated but you can try this out.

Same UICollectionViewCell and UITableViewCell in several tables / collectionViews

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.

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.

Custom UITableViewCell using storyboard with some cells

I have a TableView in which a most cells are pretty standard. I make them buy using static cells in Storyboard. However, one cell I would like to customize probably using an XIB file so I would need to load it programmatically.
In the TableView's data source, is it possible to handle loading XIB view for this particular cell only, while leaving other cells to what's delineated in the static cells in the Storyboard? Or is it an all or nothing thing where I need to just give up using static cells altogether?
The rationality for doing this is that I would like to make Storyboard to look as close to the real thing as possible. Right now if I provide a data source, the static cells in the storyboard would have no effect on the actual output and is not in any sense linked to the actual output.
Yes, it is possible. Set the custom class for the custom cell. If you wish to customise it from code, just connect it as an IBOutlet to the UITableViewController.

Resources