Hakuba
我想精简视图控制器。
我想在不编写 UITableViewDelegate
和 UITableViewDataSource
代码的情况下管理 tableview。
这就是我创建 Hakuba
的原因。
( Hakuba 是日本最有名的滑雪胜地之一。 )
功能
- 无需编写
UITableViewDelegate
和UITableViewDataSource
协议的代码 - 轻松管理您的分区和单元格(追加/重置/插入/删除/更新)
- 支持从 ios7 开始的动态单元格高度
- 无需担心单元格标识符
- 通过尾闭包处理单元格选择
- 轻松实现头部/尾部视图(悬浮回调)
- 支持从 Nibs 或 Storyboards 创建单元格
- 方法链式调用
- 下标
- 支持加载更多闭包
- 完整示例
快速示例
// viewController swift file
hakuba = Hakuba(tableView: tableView)
let cellmodel = YourCellModel(title: "Title", des: "description") {
println("Did select cell with title = \(title)")
}
hakuba[2]
.append(cellmodel) // append a new cell model into datasource
.bump(.fade) // show the cell of your cell model in the table view
hakuba[1]
.remove(1...3)
.bump(.Right)
// your cell swift file
class YourCellModel: CellModel {
let title: String
let des: String
init(title: String, des: String, selectionHandler: @escaping (Cell) -> Void) {
self.title = title
self.des = des
super.init(YourCell.self, selectionHandler: selectionHandler)
}
}
class YourCell: Cell, CellType {
typealias CellModel = YourCellModel
@IBOutlet weak var titleLabel: UILabel!
override func configure() {
guard let cellmodel = cellmodel else {
return
}
titleLabel.text = cellmodel.title
}
}
使用说明
- 初始化
private lazy var hakuba = Hakuba(tableView: tableView)
- 章节处理
let section = Section() // create a new section
// inserting
hakuba
.insert(section, at: 1)
.bump()
// removing
hakuba
.remove(at: index)
.bump(.left)
hakuba
.remove(section)
.bump()
hakuba
.removeAll()
.bump()
// handing section index by enum
enum YourSection: Int, SectionIndexType {
case top
case center
case bottom
static let count = 3
}
let topSection = hakuba[YourSection.top]
- 单元格处理
// 1. appending
hakuba[0]
.append(cellmodel) // append a cellmodel
.bump(.fade) // and bump with `Fade` animation
hakuba[1]
.append(cellmodels) // append a list of cellmodes
.bump(.left)
// by using section
let section = hakuba[YourSection.top]
section
.append(cellmodel)
.bump()
// 2. inserting
section
.insert(cellmodels, at: 1)
.bump(.middle)
// 3. reseting
section
.reset(cellmodels) // replace current data in section by the new data
.bump()
section
.reset() // or remove all data in section
.bump()
// 4. removing
section
.remove(at: 1)
.bump(.right)
section
.remove(range: 2...5)
.bump()
section
.removeLast()
.bump()
// updating cell data
let section = hakuba[YourSection.top]
section[1].property = newData
section[1]
.bump()
section.sort().bump()
section.shuffle().bump()
section.map
section.filter
section.reduce
section.mapFilter
section.each
section.first
section.last
section[1]
section.count
- 注册单元格、页眉、页脚
hakuba
.registerCellByNib(CellClass.self)
hakuba
.registerCell(CellClass.self)
hakuba
.registerHeaderFooterByNib(HeaderOrFooterClass.self)
hakuba
.registerHeaderFooter(HeaderOrFooterClass.self)
// register a list of cells by using variadic parameters
hakuba.registerCellByNibs(CellClass1.self, CellClass2.self, ..., CellClassN.self)
- 章节页眉/页脚
let header = HeaderFooterViewModel(view: CustomHeaderView) {
println("Did select header view")
}
hakuba[Section.top].header = header
- 加载更多
hakuba.loadmoreEnabled = true
hakuba.loadmoreHandler = {
// request api
// append new data
}
- 提交编辑
hakuba.commitEditingHandler = { [weak self] style, indexPath in
self?.hakuba[indexPath.section]
.remove(indexPath.row)
}
- 取消选择所有单元格
hakuba.deselectAllCells(animated: true)
- 单元格类中的回调方法
func willAppear(data: CellModel)
func didDisappear(data: CellModel)
安装
- 使用 CocoaPods 安装
pod 'Hakuba'
- 将所有文件复制到您的项目中
- 使用子模块
系统要求
- iOS 9.0+
- Xcode 9+
- Swift 4
许可证
Hakuba 采用 MIT 许可证发布。详情请参阅 LICENSE 文件。