UICollectionView that functions like a tableview - ios

The designer came back with a layout that displays information differently.
Is it possible to make my UICollectionView function like a UITableView? Where each cell takes up one row and there is only one column?
Everything I found on the net seems to be about the opposite, making a UITableView be a UICollectionView

Actually, its very simple. Just make the width of the UICollectionViewCell equal to the width of the view.

Related

Collection view Cell layout issue

I am designing an app that has the following layout. I was thinking of having the view as a collectionview.
A and B are a custom collection cells. How would I design the app so that the second and third rows have 2 and 3 cells as shown in the image?
I tried over-riding the sizeForItemAt indexPath method and then calculating the size of each cell based on the row it is in.
Not only it seems a little odd and but the cells don't align properly either.
I tried setting the estimatedItemSize property as well as over-riding preferredLayoutAttributesFitting by following this link.
Is there an easier way to do this UI ? Should I remove colleciton view and go with plain ol tableviews ?
Any help would be much appreciated.

How to align UICollectionViewCells from left to right?

I should design UICollectionView like below image.
It was almost done. but my UICollectionView is displaying like below image.
I thought it would be solved if I set semanticContentAttribute of UICollectionView to forceLeftToRight. but the result was not changed.
How can I design that like first image?
forceLeftToRight simply forces a left to right layout even when the locale is a right to left locale, so this doesn't help since UICollectionViewFlowLayout centers the cells within its view.
The complicated way to do what you want is to subclass UICollectionViewFlowLayout and override layoutElementsForItem(at:) to place the last cell on the left.
The simpler way is to add an additional invisble/empty cell in the case where there is an odd number of cells.

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.

Collectionview CustomLayout

I want a collectionview cell should be the dynamic height. I am getting data from server and loading in to tableview inside collectionview cell. That cell height should be change according to the table cell height, I am getting till here. But the problem is The cells are not aligning from the top. see this pic. The cell should create from top. This is demo code. Thanks
If I'm understanding your problem correctly, try setting self.yourCollectionView.automaticallyAdjustsScrollViewInsets = false
You will need to extend UICollectionViewLayout. And then in the prepareLayout method, you will need to specify the calculated frames of every item of your collectionview.
There are two tutorials:
http://www.skeuo.com/uicollectionview-custom-layout-tutorial is a very good tutorial for custom layout.
https://github.com/betzerra/MosaicLayout and https://www.cocoacontrols.com/controls/rfquiltlayout is pretty much what you want. A related question about rfquiltlayout on stackoverflow is RFQuiltLayout and UICollectionView

How to dynamically resize UITableViewCell based on UITableView scrolling?

I have tried looking for an answer to this question but so far I haven't got any luck.
Context
I have a UITableView inside a standard UIViewController (NOT a UITableViewController..). I have subclassed UITableViewCell and all the cells in the tableview are from the same class.
Requirement
I would like to find an efficient way to resize the cells based on the scrolling of the tableview. For example, when the cell is at the bottom of the visible list, the height is X. When the cell moves up on the screen, its height should proportionally increase to 2X.
Additional Info
I am close to just ditch the UITableView way and start making my own control that would implement such a feat by subclassing a UIScrollView. However, I would like to see if it is possible before going this path. I did see some very interesting SO posts but nothing that would put me on the right path.
Any hint or help would be highly appreciated.
You would need to respond to the scroll view delegate method scrollViewDidScroll: and implement it to record the cell at the top of the table view and reload the table view. Then your table view delegate method tableView:heightForRowAtIndexPath: sets the cell height by the relationship between the index path and the top cell index path.
Your main issue is performance while reloading the table view all the time.
If you wanted to roll your own solution it would be along the lines of the above description anyway, but you would be able to be more efficient than the table view as the table view will call the tableView:heightForRowAtIndexPath: method once for every row for every reload in order to calculate the total height of the table content.

Resources