UICollectionViewCells with dynamic heights - ios

What would the pattern be for sizing a UICollectionViewCell based on some content I've downloaded? I wouldnt know the size when the cell gets created - it would be sometime after my content has downloaded that the cell would callback with its height. What would I call on the collectionview to have it re layout its cells?
I've faced this with tableviews and ended up calculating the height based on the strings and controls in the cell. But in that case essentially everything was a function of data already in my model. Thats not the case here.
Thanks,
Jason

Depending on the number of cells you will displaying (e.g. a small # of cells), simply calling [UICollectionView reloadData] will do the trick once you know the different heights from that network callback.

I think that the the RFQuiltLayout can solve your problem.
Use the function
(CGSize) blockSizeForItemAtIndexPath:(NSIndexPath *)indexPath
Here is the link https://github.com/bryceredd/RFQuiltLayout

Related

UICollectionView that functions like a tableview

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.

Dynamic cell height issue with UITableViewCell autolayout jerk while scrolling

I am trying to do something like loading up different type of cells with custom height in a uitableview. The tableview cells are subclassed and consists of labels with the respective constraints. Each cell is having a dynamic height.
Now even before my table reloads the data, I am calculating the height that is required for the resizing of the cells and caching it in my model class so that I dont have to calculate the height when the data is rendered on the device.
To calculate height i did use the tutorial from Ray Wenderlich and I am having the right set of heights applies to the objects.
Now the problem comes. Whenever I am dequeueing the cells there is a
kind of a small jerk that gives me an indication that my cell is
dequeued while scrolling.
How can i make these movement smooth so that there is no jerk while scrolling the view ?
The height is getting assigned in and does get the value as per the current type of data getting loaded.
estimatedRowForIndexPath
Also I am calling layoutIfNeeded from my cellForAtindexPath
Suggestions are most welcome.
It's very hard to say without seeing your code in cellForRowAtIndexPath, and without seeing your cells and their respective code. Here are some general questions I would investigate:
What is the content of the cells and how complex is the view hierarchy in the cell?
Even though you are supplying the correct estimated height, an autolayout pass still needs to happen, and a complex view hierarchy will take time to resolve
Does the cell contain images?
Images that need to be decompressed from a file (UIImage imageNamed:) can be intensive and cause scrolling issues, check images are not bigger than they need to be. If needed, bump this work onto a background thread.
Are you calling a complex method to configure the cell for display in cellForRowAtIndexPath?
Look at the work actually being done in cellForRowAtIndexPath, is there a complex method being triggered in you cell subclass or view model?
Are you adding and removing views to the cell view hierarchy in cellForRowAtIndexPath?
If views are being added, removed, created, inflated from a xib, constrained etc during the cell config, this could slow things down. Try to do only what is strictly needed. Check if there is any code being run internally in the cell subclass during cellForRowAtIndexPath that could be moved to cells initWith... or awakeFromNib methods (ie code that could just run once when the cell is created, rather than every time the cell is displayed)
Also run the Instruments time profiler, see if that offers any more clues

Add views to UIScrollView only when will be visible

I am creating views at run time using heavy data, and adding these views to a UIScrollView.
The problem here is,it takes a lote of time to create the screen, which is not so good user experience.
I want to create initialy only for the part of screen that are visible, and add/create the others views when scrolling the UIScrollView.
Any tips about the best approuch?
Thanks in advance
You can use the UITableView, set cell separator to none and use variable height of row cell. Do as you always do with UITableViewCell i.e. deque cell using identifier.
This is the best approach to deal with the memory issue and for large amount of data.
Also you can do the same for UICollectionViewCell too.

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

UITableViewCells

Can tableview cells be dynamically configured so different cells can have different heights, based on data/text content? Does this violate Human Interface Guidelines? The documentation I have found is spotty and confusing, at least to newbies like me! :)
You can specify the height for each cell by implementing the tableView:heightForRowAtIndexPath: method in your UITableViewDelegate; note this will be called for every row, even rows not currently visible, so it must be fast and you may have trouble if your table has thousands of rows.
The only statement I see regarding cells with different heights in the HIG is "Avoid variable row heights in a plain table. Variable row heights are acceptable in grouped tables, but they can make a plain table look cluttered and uneven."
The UITableViewCell does not control its own height.
To set the height of a row in the table, implement the UITableViewDelegate method tableView:heightForRowAtIndexPath:
This can be somewhat painful sometimes - since often the cell is the best place to calculate its own height. The pattern I use is to implement a class method on the custom UITableViewCell class to calculate the cell's height given the data it is displaying. Call this from your implementation of tableView:heightForRowAtIndexPath:
I've never had an app rejected for variable height rows in either plain or grouped tables.

Resources