Each cell with different layout in TableView, how is scroll optimised? - uitableview

If I have a UITableView with each cell having a different layout. For example, cell 1 has one text field, cell two has two text fields and so on, in this case height of tableview cell varies based on how many text fields it has one below the other. For this specific scenario, will I be able to make use of reuseIndetifier and reuse the cell.
Is tableView reuseIdentifier useful only when layout of each cell is same? Or can I still reuse cells by making use of single identifier but adding textfields in cellForRowAtIndexPath dynamically?

I experimented a bit. I can do some basic reuse of cells by having a cell with empty template/blank cell. But in the cellForRowAtIndexPath I can dynamically create text fields and add. This works perfectly fine, allowing me to add as many number of views required to be added to TableView cell. I am using blank cell, which will be reused on scroll. However I am not sure that will be the impact on performance when I create and add UI elements dynamically to the cell in cellForRowAtIndexPath. Any ideas there?

Related

Swift 2.1 tableView cells layout and content to change in runtime

I need a tableView which could have any number of cells with different layout and/or data in different cells and could change in run time.
With static cells, I am limited by its initial layout creation and number of cells.
With prototype. I am limited by its fixed layout even though its number and data can change.
I need to select different layout as well as data and number of cells in runtime. How do I get this behaviour?
Thanks
Use tableView:numberOfRowsInSection to dynamically change the number of cells in the UITableView depending on some model you have.
Use tableView:cellForRowAtIndexPath: to dynamically select one of your UITableViewCell subclasses, which will have a layout that you define, and populate it with data.
When your model changes, you can reload the cells in the tableView and they will change according to the logic in your UITableViewDataSource methods.

UITableView dynamic cell heights - reload height for single row

I have a UITableView with rows of dynamic heights, many of which contain UITextViews. When the user starts typing in one of the textviews, I have the cells grow to accommodate the size of the textview content using the begin/endUpdates method. Using this method allows the cells to resize without losing keyboard focus on the textview, an important aspect of my app.
However, when I call begin/endUpdates, it reloads the heights for every cell that I have, and I was wondering if there was any way to only recompute the height of a cell at a particular indexPath while the user is typing. I want to do this because my heights for the other cells are expensive to compute as they have dynamic content. I know I could write some height caching code, but I was wondering if there was any method to only recompute the height of a specific cell or set of cells in a UITableView without losing keyboard focus / reloading the data content of that row?
I am using ios8, but will take an ios7/8 solutions.
Create of array of NSIndexPath's. If you have just one indexPath, add just this to the array, and use this method:
[self.tableView reloadRowsAtIndexPaths:[NSArray *array] withRowAnimation:UITableViewRowAnimationAutomatic];
but i think this method, will also hide the keyboard, never tried.
Doesn't seem like this is possible to do for only a single row. I ended up building a caching system for row heights so that at least it is faster to return heights for all cells.

Using two cells inside other in UITableView

I have few types of cell in table view and sometimes, for some field, these types are merged in one cell. For instance, I have cells for date and for simple text input, but sometimes those cells can be merged in one like here :
If any chance I can use UITableView standard pool for reusing this cells (inside one that include them).
My concern is when I get 3 cell inside tableView:cellAtIndexPath: (compound cell, date and text cell). When compound cell will be released table view will reuse date and text cell too? How tableView knows if it's use cell or not?
I know it can be done with custom layout for UICollectionView, but I want to know is that feasible for UITableView

Using a Custom UITableViewCell with Prototype Cells

For reference, this tutorial I am following: http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
I have to create a custom UITableViewCell, and from the tutorial, it said to leverage Prototyping cells and subclass UITableViewCell. Then connect the UI elements in the prototype cel to the custom class (of UITableViewCell).
The problem I am running into here is, there is only one cell for the whole table view that is displaying data. However, I am able to click on empty cells in the background behind that one cell that contains data. If i scroll up or down, cellForRowAtIndexPath is called and another cell gets displayed. However its only displaying once cell at a time for the whole table view.
Does anyone know what the problem could be here? Any help is appreciated, thanks!
Figured out what the problem is, my labels (that are supposed to be inside the custom prototyping cell) were added to the parent view and not the content view of UITableViewCell. This is because my prototype cell did not have a content view for some reason. I had to add a another prototype cell and delete the previous one.
reference: How to add subviews to a custom UITableViewCell in a storyboard

iOS how to name cell IDs?

I am confronted with the same issue as this post Iphone - cellForRowAtIndexPath behaving weirdly when scrolling down and up. I am confused how I should be using different cell IDs for different cells. I have a variety of cells:
cells with switches
cells with checkmarks
groups of cells such that only one is checkmarked
cells that change text after tapping them
cells with indicators
cells with no accessory type
Should I be using a different cell ID for each of these different types of cells?
I would use different reuse identifiers for each different type of cell. That would make it so you won't have to do things like take a checkmark cell and change it to a switch cell. You'll dequeue each type of cell and get back what you expect. Does that make sense?

Resources