Reusable identifier cells versus returning a cell subclass? - ios

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.

Related

Setting Content on 3 Separate Static TableViewCells that are of the same type

I have a tableview where a single prototype cell is used only 3 times. This cell has some buttons on it that trigger displaying and hiding content within the cell.
I could reuse the cells, but that requires a lot of resetting each cell so that if I expand A's content, B's content isn't expanded when it loads. Furthermore, this requires me to keep a state record in the Controller class, where I would prefer to have this all handled by the Cell itself for modularity. In other words, the amount of work to keep each cell in the correct stage seems inefficient.
What would be the best way to go about this? Do I use static cells? Is there a way to instance 3 separate cells of the same type and place them in the TableView?
You certainly can have the modular part of the code you need inside the cell itself. If you don't want to reuse the cells, then add a custom initializer with a case for each cell type. Inside your delegate method cellForRow:
return CustomCell(type: .myCustomType)
If you do it this way, you can add a switch inside the cell's initializer & set up the cells according to their types that way. And i'm assuming by "static" you mean just three instances. You "add" cells to the table by telling the delegate the number of cells that it will need, which will in turn call cellForRow x times.

How to access UITableView Cell which is not yet in view?

I want to access/update UITableView cell (using reusable cells) which is not in the current view . I know the table view cells are reusable, that can be the reason I am unable to fetch them but is there any way of virtually making and updating. OR I have to drop the reusable cell technique. Suppose tableview have total 20 cells but only say 7 are visible in the current view of iPhone. How will i update other 13 cells which are out of view bounds
Accessing and modifying cells (even if you could) would be a bad pattern. UITableViewCells are designed to be created and modified solely in the tableView:cellForRow:atIndexPath datasource method, where framework automatically asks you what to do with cells that are about to be displayed.
The whole idea behind reusability is that the system takes care of the view for you, and all you need to do is take care of your datasource and instruct the system, using the model, how to render cells, in its allocated datasource method.
This paradigm would be defeated if we started accessing cells manually and modifying them.
You can't access those cells because they are not added to the UITableView but are kept in a queue until user scrolls to them, then they are added to the UITableView. Instead update your model, which will reflect changes on the cells.

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.

Possible to have nested reusable UICollectionViewCells

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

configure UICollectionView cell on initial lookup

I am getting cells for a UICollectionView by calling dequeueReusableCellWithReuseIdentifier:. I want to set some specific configuration information the first time my cell is returned from this method and not subsequently when it gets reused. Is there a hook somewhere where I can run "one time" code on collection view cells?
Obviously I could just set this information every time or use a boolean to keep track of whether or not the cell has been initialized, but I'd like to know if there's a cleaner way first.
This is easy enough to do from within a cell's implementation but there's no convenient way for a data source to differentiate newly created vs reused cells. If your configuration must be supplied by the data source then the data source probably need to check if the cell has been configured already.
The cells will be created once so you can use init or awakeFromNib to set some initial state. Cells will then have prepareForReuse called when being reused allowing you to perform any changes you need to make per-use.
The way I ended up solving this was to put my own view inside a generic UICollectionViewCell with a view tag. Then, when I go to deque my cell, I pull out the view using viewWithTag. If I get nil back, it's the first time this code has run, so I can init my view using my own constructor normally. This seemed slightly better than keeping track of a boolean in the cell implementation.

Resources