一个 Swift 库,使用闭包实现 UIKit 等组件的 delegate 和 dataSource 方法
在实现 delegate 的各个方法时:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableView == leftTableView ? leftDatas.count : rightDatas.count
}
这些情况让代码不易阅读和维护。
希望:
Xcode 8, Swift 3.0, iOS 8+ 所有方法名称以ce_
开头
将 Centipede
目录复制到您的项目中即可。
使用闭包需要注意循环引用问题,Swift 使用 weak 或 unowned 关键字解决循环引用问题
delegate
和 dataSource
方法import Centipede
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView! {
didSet {
let kCellReuseIdentifier = "MYCELL"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: kCellReuseIdentifier)
tableView.ce_numberOfSections_in { (tableView) -> Int in
return 3
}.ce_tableView_numberOfRowsInSection { (tableView, section) -> Int in
return 5
}.ce_tableView_cellForRowAt { (tableView, indexPath) -> UITableViewCell in
return tableView.dequeueReusableCell(withIdentifier: kCellReuseIdentifier, for: indexPath)
}.ce_tableView_willDisplay { (tableView, cell, indexPath) in
cell.textLabel?.text = "\(indexPath.section) - \(indexPath.row)"
}.ce_scrollViewDidScroll { (scrollView) in
print(scrollView.contentOffset)
}
}
}
}
// UIControl
button.ce_addControlEvents(.touchDown) { (control, touches) in
print("TouchDown")
}.ce_addControlEvents(.touchUpInside) { (control, touches) in
print("TouchUpInside")
}
button.ce_removeControlEvents(.touchDown)
textField.ce_addControlEvents([.editingChanged, .editingDidBegin]) { (control, touches) in
print("TextChanged")
}
// UIBarButtonItem
let barButtonItem = UIBarButtonItem()
barButtonItem.action { (barButtonItem) in
print("UIBarButtonItem action")
}
// UIGestureRecognizer
let gestureRecognizer = UIPanGestureRecognizer { (gestureRecognizer) in
print(gestureRecognizer.state.rawValue)
}
self.view.addGestureRecognizer(gestureRecognizer)
Centipede采用MIT许可发布。有关详细信息,请参阅LICENSE。