How to tell apart prototype table view cells? - ios

Say I have a table view with a single prototype cell in it that contains a stack view. I'd like each cell of the table view generated from the prototype cell to have different views in the stack view. So, I need to find a way to tell table view cells apart.
If the table view had static cells, I could just use the tag property of the cell and populate stack views easily. Though, using static cells is probably not the best approach since cells are almost identical to each other.
What can I do to populate stack view differently depending on the "row" of the table view cells?

That's precisely what you should be doing in your tableView:cellForRowAtIndexPath: method.

Related

UITableView cells on the same row

as I say in the title, I'd like to display two or more cells on the same row. I have every cell with a UILabel inside. The labels can have changing width depending on user's input. I'd like to fill a row with many labels as I can. Can someone give me some advice on how to do it? Thanks.
Short answer: You can't. Slightly longer answer: Use a collection view.
Table views are designed to show a single column of cells.
You can't make a table view contain multiple cells in a row.
You could probably create a view controller that had side-by-side table views inside it, but it would be awkward and hard to use, and the table views would scroll independently of each other.
If you want multiple cells on the same row, you want a collection view. A collection view is similar to, but more flexible than, a table view.
I would consider using a horizontal UIStackView to achieve what you're trying to do. Depends on how dynamic and changeable you need it to be.

How to create 2 steps expandable tableview Swift

There is a main table view and inside each of the main table view cell has their own header table view. Then each of the header table view cell has their own sub-header table view. All the table rows don't have fixed counts according to the dynamic data. What I try to achieve is when a cell of the main table view is expanded, I should see the header table view cells unexpanded. After a header table view cell is expended, I should see the sub-header table view. I need a solution without using libraries.
I provided the storyboard UI based on what I want to achieve.
To deal with expandable cells (or cells inside cells effect) I recommend using UIStackView inside UITableViewCell and add inside needed views with data.
For example you can create UIView inside Xib file and load it inside needed UITableViewCell then add to UIStackView and fill with data.
Then you can simply hide/unhide elements inside UIStackView to archive expanding/unxpanding.
This way or other you should use 1 UITableView.
I once made a 1 step expandable Table View.
Here is what I did to achieve that.
Do not make different table views for each hierarchy. That will be difficult to manage. Instead, make one table view but change the type of cell you want to show. If the cell is top level cell or first level cell or the second level cell, change the design of each one that.
The data source will then have list of list of list to denote the 2 level expandable UITableView.
To open and close the table view, you can simply change the data source and reload or use beginUpdates and endUpdates

Can we add tableview to cell?

I want to customize my tableview. I want to add another tableview in cell. Can we add tableview in cell of another tableview?
Please give me some ideas.
You can embed a UITableView into another UITableView's cell, but due to potential issues with the embedded UIScrollViews it is not recommended.
If you feel like you need a table in a table, first consider if it is not possible to achieve the desired behavior by using sections and cells in the sections (a section in a table might represent the top tableView's cell, and cells in the given section would represent the embedded tableView's cells).
There is no problem with adding a UITableView into a a UITableViewCell, since both of them are UIViews its simply would be translated to adding a subview to a view...
However, keep in mind to consider the following issues:
1- From a user experience perspective, that would be a vertical scroll view inside another vertical scroll view (are you ok with that?).
2- For each cell -in the main container table view-, you would need to handle the table view delegates/datasources. For understanding the logic of how this could be done, you might want to check:
Is it possible to add UITableView within a UITableViewCell.
Putting a UICollectionView in a UITableViewCell in Swift: although it is about adding a UICollectionView inside the cell, it helps to gain the logic of how to achieve such a task.

How can I place a view between cells in collectionView?

I want to place a view between cells in a collectionView. Like an ad. to be added dynamically after they see the first 3 cells. Is there a method to do that?
If you could figure out a way to do that you still shouldn't. A collection view owns the area it draws in. It's in charge of placing cells where they belong within it's content view, and you are supposed to keep out.
That said, collection views are extremely flexible and you could design your collection view to display a set of "normal" cells, an ad cell, and then more normal cells. If your ad cells are different sizes or need to be spaced differently than your normal cells then you might have to create a custom collection view layout.
No, you can't. But you can add a UICollectionViewCell which will contain advertisements or something else you want to be added. It is really simple. Just create two prototype cell. First cell will contain your data, second one will contain some extra data.

Best approach to achieve this grid layout on iOS

A question for the UICollectionView experts.
Imagine a collection view that looks like a table view (full width cells), and when you tap on one of them, new cells are inserted underneath that cell that are square cells, say half the width of the collection view, in 2 columns. Tapping the header again would appear to collapse the section.
Keeping in mind that I’m trying to use UICollectionViewFlowLayout instead of a custom UICollectionView.
Would you:
A) implement the tableview style cells as collection view supplementary views (headers), with a gesture recogniser that inserts the square cells?; or
B) implement the tableview style cells as one cell type, and the square cells as another cell type?; or
C) something else?
Personally I would go with option A. In the situation you're describing the full width item is really a section header that's function is to show/hide it's sections's cells.
From a data structure standpoint this is much easier to maintain with this approach. It can be handled with an array of arrays.
If they were all cells it would most likely be one large array, and would be more difficult to make the distinction between a full width cell and the half width cells.

Resources