Table&Viewsyou're'doing'them'wrong
©"Robert"Brown"March"2015"@robby_brown
Overview
1. The&Hard&Way
2. Code&Architecture&Theory
3. The&Be7er&Way
©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'onclass CandyTableViewController : UITableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.candies.count }
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell let candy = self.candies[indexPath.row] cell.textLabel!.text = candy.name cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell }}
From:&raywenderlich.com
©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'onWhat's'wrong'with'this?
• Duplicate+boilerplate
• View+controllers+handle+interac5on,+not+data+sources
• Cells+are+o8en+shared+between+table+views
• Cells+should+handle+their+own+layout
©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'onWhy$do$we$see$this$so$o,en?
• It's&"standard"&prac.ce
• That's&how&I've&always&done&it
• My&favorite&tutorial&does&it
• Apple&does&it
©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'onWhy$do$we$see$this$so$o,en?
• It's&really&convenient&for&tutorials&to&be&simple
©"Robert"Brown"March"2015"@robby_brown
Data$SourcesProblems:
• Massive(View(Controller
• Duplicate(boilerplate
©"Robert"Brown"March"2015"@robby_brown
Data$SourcesSolu%on:
• Single(responsibility0principle
• Combinators
• Chain(of(responsibility
©"Robert"Brown"March"2015"@robby_brown
A"class"should"have"only"one"reason"to"change.
—"Single)responsibility"principle
©"Robert"Brown"March"2015"@robby_brown
A"class"should"have"only"one"reason"to"change.
—"Single)responsibility"principle
©"Robert"Brown"March"2015"@robby_brown
Every&class&should&have&responsibility&over&a&single&part&of&
the&func6onality...,&and&that&responsibility&should&be&en6rely&
encapsulated&by&the&class.1
—"Single)responsibility"principle
1"Wikipedia
©"Robert"Brown"March"2015"@robby_brown
Combinators• Technique*from*func/onal*programming
• Usually*seen*with*parsers
• Generally*represent*a*single,*simple*feature
• Focus*on*composi/on
• Like*higher>order*func/ons*applied*to*features
©"Robert"Brown"March"2015"@robby_brown
Chain&of&Responsibility• Like&a&chain&of&delegates
• A&3>&B&3>&C&3>&D&...
• If&A&can't&handle&something,&then&try&B&...
• Used&to&create&a&sequence&of&combinators
©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'onView%Controller
override func viewDidLoad() { super.viewDidLoad() let objects = [] // Get objects here tableView.dataSource = BaseDataSource(objects) { (view, indexPath, object) -> Any in return MyCell.cell(view as! UITableView, name: object as! String) }}
©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'onView%Controller
override func viewDidLoad() { super.viewDidLoad() let objects = [] // Get objects here let base = BaseDataSource(objects) { (view, indexPath, object) -> Any in return MyCell.cell(view as! UITableView, name: object as! String) } self.dataSource = ReorderableDataSource(base)}
©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'onCell
class MyCell: SmartTableViewCell { @IBOutlet weak var titleLabel: UILabel!
class func cell(tableView: UITableView, name: String) -> Self { let cell = self.cell(tableView) cell.titleLabel.text = name return cell }}
©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'onWhy$is$this$be*er?
• Fewer&lines&of&code
• Boilerplate&removed
• Behavior&can&be&changed&with&the&data&source(s)
• The&data&source&can&work&with&table&and&collec=on&views
• Easier&to&build&combinators
©"Robert"Brown"March"2015"@robby_brown
Dynamic(Behaviors• Array
• Mul)*dimensional2array
• Filtering
• Reordering
• Edi)ng
• Index2)tles
©"Robert"Brown"March"2015"@robby_brown
Considera*ons• The%order%of%combinators%applied%ma4ers%for%performance
• Ex:%Create%index%9tles%before%filtering
• Some9mes%a%combinator%needs%to%implement%more%than%one%feature%for%performance
©"Robert"Brown"March"2015"@robby_brown
Example(Combinators
• BaseDataSource:#Provides#minimum#func1onality
• ChainableDataSource:#Allows#data#source#sequences
• FilterableDataSource:#Allows#filtering#(ex.#search#bar)
• ReorderableDataSource:#Allows#reordering
• IndexableDataSource:#Shows#index#1tles
©"Robert"Brown"March"2015"@robby_brown
Summary
1. The&Hard&Way
2. Code&Architecture&Theory
3. The&Be7er&Way
©"Robert"Brown"March"2015"@robby_brown
Resources(to(Learn(More• Source(Code
• Combinator
• Func2onal(Programming(in(Swi7
• Func2onal(Snippets
©"Robert"Brown"March"2015"@robby_brown
Resources(to(Learn(More• Separa'on*of*Concerns
• Cohesion
• Orthogonality
©"Robert"Brown"March"2015"@robby_brown