Possible to have nested reusable UICollectionViewCells - ios

I take great care to use custom reusable UICollectionViewCells with a ReuseIdentifier so that cell subviews do not have to be recreated but only have to be refilled and resized with new data each time.
I now have a complex custom UICollectionViewCell with a group of subviews that can get repeated X times, depending on the data. So a part of the cell can be repeated 1 to n times. This part has a couple of subviews and right now I recreate them every single time if I fill a reused cell with new data. This creates a heavy processing load and is quite dumb.
Is it possible to create a separate UICollectionViewCell with only this subpart and then include it with a ReuseIdentifier X times in my parent UICollectionViewCell thus benefiting from reusing this subpart and not having to recreate all the subviews of this subpart every single time?
Or in simpler terms, is it possible to nest a UICollectionViewCell in UICollectionViewCells and benefit from the reusing mechanism of iOS?

You could nest a UICollectionView (or UITableView) within a UICollectionViewCell. This might be simpler than the subview scheme you are using now.
Your UICollectionViewCell would need to implement the delegate and data source methods for the nested collection view (or table view).
Do not confuse the nested collection view with the collection view to which your parent UICollectionViewCell belongs. Don't mix reuse identifiers, in other words.

You can call the [UICollectionView refreshdata] in the dequeue method for (outer) UICollectionView

Related

Reusable identifier cells versus returning a cell subclass?

I create my table view cells in xib files that I then register with my table view and return in cellForRowAt using the tableView.dequeueReusableCell method. In the rare instance I create cells by subclassing them and manually programming the interface I usually just initialise them and return them from within cellForRowAt.
I recently discovered that you can register subclasses using tableView.register(cellClass: AnyClass?, forCellReuseIdentifier: String). Should I be registering my subclasses and returning them via dequeueReusableCell? What are the benefits of using it instead of returning an initialised subclass?
You have to always register cell(via code or in storyboard). The Reusability principle is the most important in iOS Table and Collection views.
It means that the table view draws and stores in the memory only few cells that are currently visible + several that mat be visible in the nearest future. If you will not use reusability you will have a big performance problems with big amount of cells.
Also don't forget to clean cells ui in prepareForReuse method in cell subclasses
Yes, you should be taking advantage of the cell reuse system. The reuse system allows the system to very rapidly respond to scroll actions on your table. Instead of having to instantiate a whole new cell from scratch the system can just take cells it already has and update their content.
Bypassing that system by making a whole new cell every time is not an ideal use of resources and with more complex cells can result in noticeable lag on your table view.

Convert Complex UITableView to UICollectionView

I currently have a prototyped UITableView in my storyboard, which has many cell with very complex layout. Now, I need to convert it into a UICollectionView, because I always have some problem with the way UITableView handles cell layout(See my this question). I figured the code part to be the easy one, as I only need to tweak the inheritance of my cell classes, as well as switch the delegate and datasource with previously written code. However, I am a little stuck on the storyboard side, that is, given all my cells are complex in layout and even copying and pasting them from one cell to another would require many layout tweaking and IBOutlet/IBAction reconnecting. Is there a fast way to convert those UITableViewCells designed in storyboard into UICollectionViewCells?
I think, you save data into one class, In CollectionView, You get data from class. You remember, tableview and collection are reverse about cell and row.

Swift custom cells layout TableView decision

I need to display a table with in my iPhone app:
neither the number of cells nor the contents are known at compile time, but only at run time.
Views for each cell may differ, one cell has textField and another may have some other view control.
Should I consider Static or prototype cells?
Should I consider tableViewController or viewController with tableview in it?
Any thing I need to consider before I start coding? Thanks in advance
For The issue of dynamic Number of cell at Run time, you can call reload data for table view at any time you have the data source ready.
Prototype Cells should be used with no problem.
Simple Table View will be sufficient for the task.
You have to make cell, either in code or in storyboard, for each type of cell you want, 1 table View can have multiple types of prototype cells, Just name them differently and then make the objects of only the specific cell of which the data is best suited.
It is not that difficult but do handle the data source with extreme care.
Should I consider Static or prototype cells?
If you know all possible subview combinations that your cells might need to display the data appropriately, and they are relatively few, make one prototype for each. for example:
One text field,
Two labels,
One image view and a label,
...etc.
Otherwise, just use the plain-vanilla UITableViewCell (no subclassing) and remove/add subviews at runtime when reusing them (that is, inside the method -tableView:cellForRowAtIndexPath:).
Should I consider tableViewController or viewController with tableview
in it?
The only reason I would choose UIViewController + UITableView over UITableViewController is if -for example- I needed the table view to only take up part of the main view's bounds/screen, and display some other subview in the remainder. Otherwise, you get so much "for free" with UITableViewController that there's just no point in implementing all of that from scratch.
You have to choose prototype cell, u can create different types of cell depending upon your requirement.Any think ok for u, u can create tableview controller or view controller.

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.

Custom UITableViewCell - subclass UITableViewCell with xib vs using tags

In order to implement a custom UITableViewCell, I know of two options:
Create a prototype cell in the storyboard view and assign tags to the cell's constituent views
Create a subclass of UITableViewCell with an associated xib
Using the tags seems simpler. I see that it has a couple of disadvantages including the inability to re-use the custom cell in more than one scene and potentially a lot of cell specific code in the view controller which could be an issue with a bunch of different cells.
For one or two cells that do not need to be reused in multiple scenes, does anyone know of any other considerations that I should take into account that might lead me to prefer a UITableViewCell subclass?
My recommendation is to use a UITableViewCell subclass with a storyboard prototype cell. Simply link your constituent views to IBOutlet properties as you would with any other class.
If you want to use the same cell in multiple tables then you need to re layout the prototype cell in each tableView, but you can cut and paste. Using tags results in unnecessary code to get references to the UI elements.

Resources