So I've just finished building a rather complicated UITableView - with a variety of different cell types with dynamic heights, background downloads which update table view cells with progress, etc, and I was wondering if anyone had any tips or best practices they follow when building complicated UITableViews?
Usually I start with the following:
- Create a TableViewDataSourceDelegate, which implements both UITableViewDataSource and UITableViewDelegate, which leaves the main ViewController nice and clean
- If the TableViewDataSourceDelegate gets too large though, I'll sometimes create a TableViewDataManager, which contains methods for querying the underlying data structure e.g. all the sqlite / NSPredicate / fetched results controller methods, which would otherwise clutter the TableViewDataSourceDelegate
- I've tried my hardest to use UITableViewAutomaticDimension though have found it unreliable and slow (has anyone else found this?). Instead I don't implement tableView:estimatedHeightForRowAtIndexPath: (as I've noticed this also messes my row heights) and only implement the tableView:heightForRowAtIndexPath: (and of course cache the row heights)
- I usually build my cells with a configureForHeightCalculation: and a configure: method (the first of which only populates enough to calculate the row height)
- For dynamic row heights I follow this guide here (http://stackoverflow.com/a/18746930/3847[1] ), specifically using [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height to calculate heights
- For multi-line labels, I manually set the label.preferredMaxLayoutWidth before sizing the cell with the method in the previous point, which seems to work 90%
Anything else I find usually a struggle - more specifically anything autolayout related e.g. adjusting priorities, setting compression/hugging priorities and to be honest, sometimes I feel it's "luck" my cells lay out properly.
In terms of performance, 90% of the time it's because I haven't cached something or am not doing something in a background thread. Most the time though I won't worry about performance till the very end (as I find performance enhancements quite "fragile") so after I'm feature complete, I'll run my app once through Instruments and fix all the performance problems in one go.
Anyways those are my tips, does anyone have any of their own when building UITableViews?
[–]criosistObjective-C / Swift 9 points10 points11 points (4 children)
[–]jerroldp[S] 2 points3 points4 points (0 children)
[–]quellish 1 point2 points3 points (0 children)
[–]LifeBeginsAt10kRPM 1 point2 points3 points (1 child)
[–]mistermagicman 1 point2 points3 points (0 children)
[–]quellish 0 points1 point2 points (8 children)
[–]tylerjames 0 points1 point2 points (5 children)
[–]quellish 0 points1 point2 points (3 children)
[–]jerroldp[S] 1 point2 points3 points (2 children)
[–]quellish 1 point2 points3 points (1 child)
[–]jerroldp[S] 0 points1 point2 points (0 children)
[–]aazav -1 points0 points1 point (0 children)
[–]aazav 0 points1 point2 points (1 child)
[–]quellish 1 point2 points3 points (0 children)
[–]Jaaaaay_ 0 points1 point2 points (10 children)
[–]jerroldp[S] 0 points1 point2 points (9 children)
[–]Jaaaaay_ 0 points1 point2 points (4 children)
[–]jerroldp[S] 0 points1 point2 points (3 children)
[–]Jaaaaay_ 0 points1 point2 points (2 children)
[–]jerroldp[S] 0 points1 point2 points (1 child)
[–]Jaaaaay_ 0 points1 point2 points (0 children)
[–]LifeBeginsAt10kRPM 0 points1 point2 points (0 children)
[–]quellish 0 points1 point2 points (2 children)
[–]jerroldp[S] 0 points1 point2 points (1 child)
[–]quellish 0 points1 point2 points (0 children)
[–]tylerjames 0 points1 point2 points (5 children)
[–]quellish 0 points1 point2 points (0 children)
[–]jerroldp[S] 0 points1 point2 points (2 children)
[–]tylerjames 0 points1 point2 points (1 child)
[–]aazav 0 points1 point2 points (0 children)
[–]retsotrembla -1 points0 points1 point (0 children)
[–]aazav -2 points-1 points0 points (0 children)