Swift 2.1 tableView cells layout and content to change in runtime - ios

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.

Related

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

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?

Keeping reuse capabilities of UITableView when it is nested in a UIScrollView

To implement a rather intricate design of a screen in an iOS app, I have a UITableView nested inside of a UIScrollView.
To keep the logic simple, I implemented a method on the UITableView that calculates its entire height, and i use the result of that method and set a constraint on the nested table view, so that the scrolling logic can be solely on the UIScrollView to deal with. (I forward methods such as scrollRectToVisible from the UITableView to the UIScrollView)
While this works great with small data sets, I have recently discovered the the reuse capabilities of the UITableView are not used, because the framework believes the entire UITableView to be visible when I set that height constraint. A simple log method in the cellForRowAtIndexPath method shows all cells get calculated at once.
My question is, is there anything I can do where I would be able to tell the nested UITableView how much of it is actually visible on screen, and to only compute those visible cells?
I basically need to override whatever part of UITableView that is responsible for calculating what cells should be visible on screen.
The table view will think of itself as filling its whole frame with cells. If you limit the height it will limit the cell count visible. Are you using the deque with reuse identifier method (if not see below)
How can I recycle UITableViewCell objects created from a XIB?

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

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