Best approach to achieve this grid layout on iOS - 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.

Related

How to reduce padding between each table view cell

Is it possible to reduce the padding surrounding each table view cell in a table view? I understand I could do this through a custom solution but I would like to have the default auto-resizing behavior of a standard table view cells but just reduce the padding between cells.
I'm not sure if I completely understand your question, but when working with table view cells, I generally
Get rid of the line operator
Set the background color of the cell and the table view to be the same
Create a subview that is essential all the design of the cell
By doing this I can make the subview a different color and increase or decrease its size, and in a way, it acts as padding between cells.
Again I'm not totally sure if that helped, but if it and you would like the code for that just respond to my answer.

Complex cross layout architecture and separation of concerns

I want to implement a cross layout just like Wallapop app does on its main feed.
As you can see, it's composed of two groups of cells (Featured items, Items near you). The first group is scrolled horizontally, and the second group is scrolled vertically.
The first UIKit component that came to my mind to make that kind of layout is UICollectionView, having one section for each scrolling direction. Unfortunately UICollectionView current implementation is very limited, forcing the scroll in one direction only, no matter how many sections you declare.
So I wanted to give it a shot with vanilla UIKit components and that's what I got;
The problem with my solution is that the vertical UICollectionViewController (highlighted in green) is scrolling on its own and not pushing the horizontal UICollectionViewControllers upward.
I've also thought about using a single UICollectionViewController for the vertical cells, and setting an UIStackView with horizontal UICollectionViewControllers as needed for the horizontal cells, but it's a messy solution and doesn't scale very well, I even couldn't set a title for the vertical cells section If I opt this way.
Ideally, I want each group of scrollable cells to be it's own UIViewController in order to have a clear separation of concerns and modularity.
Is there a better way to implement a layout like the one I want with vanilla UIKit components?
The easiest and the most stable solution is to use the following view hierarchy:
Use one UICollectionView(1) instance
Horizontally scrollable sections can be implemented as Screen width UICollectionViewCell containing horizontally scrollable UICollectionView
Vertically scrollable sections should be just a regular section of UICollectionView(1)
Pros
UIKit only
Nothing extraordinary is needed - just a UICollectionViewFlowLayout everywhere
Cells are the same for Horizontally scrollable cells and Vertically scrollable cells
Good scalability and separation of concerns. Independent behaviors of Horizontal and Vertical sections each of which can have multiple data sources.
Cons
Horizontally scrollable sections should have fixed height. Otherwise scrolling behavior will be harder to maintain.
Arrow on the image means Uses!
You could use following way:
A collection view with two cell in it. First cell is for featured items and the second cell is for items near you. The first cell will cover full width and height required by your design.
For scalability use a Container View which embed a CollectionViewController which will responsible for showing featured item. This embedded CollectionViewController will scroll horizontally.
Items near you are straight forward direct cell of the initial collection view controller.
So your implementation of featured horizontal collection view remain in the embedded CollectionViewController and the items for cell remains in the primary CollectionViewController which also contain a loose connection (embedding into a container view) to the featured item.
It also does not suffer from the problem like scrolling vertically the items for sale does not scroll up the featured items.
Here is a screenshot which depicts the idea

iOS StackView or CollectionView

I have to make a grid with a specific architecture.
I don't know if I have to use a stackView with views? Or a collectionView with cell?
Let's see my image :
Green cells will be repeated infinite times.
Thanks for your help!!
The easiest and best way do make this is collection view, Also in collection views cells(items) are reused, so you don't need to release them after they have disappeared.
It depends on the number of grid cells. If the number of cells is 2~4, it's OK to use stackView.
But
Green cells will be repeated infinite times.
So, you should use collection view. Collection view is suitable for displaying repeating layouts like your image or calendar app on iOS.
And offscreen collection view cell(table view cell also) is reusable, so you only create cells enough to fill the screen and a little more.
See Table View Cell Reuse Explanation

UITableView fixed section with dynamic size

I am trying to implement something like this:
I have a UITableView with 1. "static" section, the total number of sections in the tableView can vary. I always want the first section (white area) to be visible to the user, the remaining sections should scroll underneath the first section. I have tried to implement this with two UITableViews, but since the (white area) can vary in size, I can't set a definite frame. I am using Storyboards with autolayout. At the moment the best solution I have come up with is the two UITableViews, but I need to find a way that I can resize the two tableViews according to the content of the white area and according to each other. The white area, one of the tableViews is containing a section with two rows, the first row is containing text that can vary in length and therefore needs dynamic resizing.
Any Idea how I can tackle this? Can I change the NSLayoutConstraints dynamically somehow?
If i understand correct, your first UIView rectangle if fixed, bottom table is scrollable.
To implement this, you should create typical UIViewController, add UIView and UITableView (programmatically or through an outlets). You should manage each view (tableView and UIView) separately.

UICollectionViewFlowLayout sections as columns rather than rows

I'm trying to create a UICollectionView of two columns (of potentially different lengths) with the property that when a cell is deleted the cells below (rather than the cells to the right) move up to take its place. I have considered customising the layout using layoutAttributesForElementsInRect etc but don't want to do this if there's an easier way.
Basically all I need is the transpose of the standard FlowLayout with each section a new column instead of a new row. Any advice would be much appreciated.
It is not possible using UICollectionView without customizing the collection view layout based on your needs. UICollectionViewFlowLayout is a line breaking layout that means, it places all your cells along a line and break that line to the bounds of the collection view one by one. So you can place two cells in a row by adjusting the cell size and section insects. Even though it wont allow you adjust the cell during the deletion as your need. Obviously the adjacent cell will take place the position when deleting a cell. Can you go with two collection views side by side instead of one??

Resources