I want to manage tableview without the code of UITableViewDelegate and UITableViewDataSource.
That is why I created Hakuba.
( Hakuba is one of the most famous ski resorts in Japan. )
Features
Don’t have to write the code for UITableViewDelegate and UITableViewDataSource protocols
Easy to manage your sections and cells (append/reset/insert/remove/update)
Support dynamic cell height from ios7
Don’t have to worry about cell identifier
Handling cell selection by trailing closure
Easy to implement header/footer view (floating callback)
Support for creating cells from Nibs or Storyboards
Method chaining
Subscript
Support loadmore closure
Complete example
Quick example
12345678910111213
// viewController swift filehakuba=Hakuba(tableView:tableView)letcellmodel=YourCellModel(title:"Title",des:"description"){println("Did select cell with title = \(title)")}hakuba[2].append(cellmodel)// append a new cell model in datasource.slide(.Fade)// show the cell of your cell model in the table viewhakuba[1].remove(1...3).slide(.Right)
123456789101112131415161718192021222324
// your cell swift fileclassYourCellModel:MYCellModel{lettitle:Stringletdes:Stringinit(title:String,des:String,selectionHandler:MYSelectionHandler){self.title=titleself.des=dessuper.init(YourCell.self,selectionHandler:selectionHandler)}}classYourCell:MYTableViewCell{@IBOutletweakvartitleLabel:UILabel!overridefuncconfigureCell(data:MYCellModel){super.configureCell(data)ifletcellmodel=dataas?YourCellModel{titleLabel.text=cellmodel.title}}}
Usage
Initilization
1
privatevarhakuba=Hakuba(tableView:tableView)
Section handling
123456789101112131415161718192021222324
letsection=hakuba[secIndex]// retrieve a section or create a new section if it doesn't already exist// insertinghakuba.insert(section,atIndex:1).slide()// removinghakuba.remove(index).slide(.Left)hakuba.removeAll().slide()// handing section index by enumenumSection:Int,MYSectionIndex{caseTop=0caseCentercaseBottomvarintValue:Int{returnself.rawValue}}lettopSection=hakuba[Section.Top]
// 1. appendinghakuba[0].append(cellmodel)// append a cellmodel.slide(.Fade)// and slide with `Fade` animationhakuba[1].append(cellmodels)// append a list of cellmodes.slide(.Left)// by using sectionletsection=hakuba[Section.Top]section.append(cellmodel).slide()// 2. insertingsection.insert(cellmodels,atIndex:1).slide(.Middle)// 3. resetingsection.reset(cellmodels)// replace current data in section by the new data.slide()section.reset()// or remove all data in section.slide()// 4. removingsection.remove(1).slide(.Right)section.remove(2...5).slide()section.removeLast().slide()
// create a cell modelletcellmodel=MYCellModel(cellClass:YourCell.self,userData:celldata){println("Did select")}// create a list of cell models from api resultsletitems=[...]// or your data from APIletcellmodels=items.map{item->MYCellModelinreturnMYCellModel(cellClass:YourCell.self,userData:item){println("Did select cell")}}
Register cell, header, footer
1234567
hakuba.registerCellNib(CellClassName)hakuba.registerCellClass(CellClassName)hakuba.registerHeaderFooterNib(HeaderOrFooterClassName)hakuba.registerHeaderFooterClass(HeaderOrFooterClassName)// register a list of cells by using variadic parametershakuba.registerCellNib(CellClass1.self,CellClass2.self,...,CellClassN.self)
Section header/footer
1234567
letheader=MYHeaderFooterViewModel(viewClass:CustomHeaderView.self,userData:yourData){println("Did select header view")}hakuba[Section.Top].header=header// hide header in section 1hakuba[Section.Center].header?.enabled=false
Loadmore
12345
hakuba.loadmoreEnabled=truehakuba.loadmoreHandler={// request api// append new data}
Dynamic cell height : when you want to enable dynamic cell height, you only need to set the value of estimated height to the height parameter and set dynamicHeightEnabled = true