11# Centipede
22
3- 一个纯Swift实现的库 ,使用闭包实现UIKit组件的delegate和dataSource方法
3+ 一个Swift库 ,使用闭包实现UIKit组件的delegate和dataSource方法
44
55### 解决什么问题
6- delegate很好的解决的自定义与耦合问题,但在实现delegate的各个方法时,方法遍布整个ViewController很散。
7- 并且如果当前ViewController中有多个UITableView或其它实现delegate的组件时,在delegate实现方法中需要判断当前触发的组件是哪个。如:
6+ 在实现delegate的各个方法时:
7+
8+ - 方法遍布整个ViewController,很散。
9+ - 具体的实现与成员变量被分开了,阅读时需要分开查看。
10+ - 如果当对象中实现多个UITableViewDataSource时,方法中需要判断组件来做出反应。如:(这很丑)
811
912``` swift
1013@objc func tableView (tableView : UITableView, numberOfRowsInSection section : Int ) -> Int {
1114 return tableView == leftTableView ? leftDatas.count : rightDatas.count
1215}
1316```
1417
15- 这让代码不易维护和阅读 。
18+ 这些情况让代码不易阅读和维护 。
1619
17- 希望:
20+ ** 希望:**
1821
1922- 代码连续。组件的构造、样式设置和各delegate实现方法可写在一个位置。
2023- 独立。有多个UITableView时,tableViewA和tableViewB的delegate方法实现是独立的,互不干扰。
2124
2225### 使用
2326
27+ - Xcode 7.0+
28+ - iOS 8+
29+ - 所有方法名称以` ce_ ` 开头
30+
31+ #### 直接源码
32+
2433将` Centipede ` 目录复制到您的项目中及可。
2534
26- - \> = iOS 7
27- - 所有方法名称以` ce_ ` 开头
35+ #### CocoaPods
36+
37+ ```
38+ platform :ios, '8.0'
39+ use_frameworks!
40+
41+ pod 'Centipede'
42+ ```
43+ code
44+
45+ ``` swift
46+ import Centipede
47+
48+ button.ce_addControlEvents (UIControlEvents.TouchUpInside ) { (control, touches) -> Void in
49+ println (" TouchUpInside" )
50+ }
51+ ```
2852
2953### 注意
3054
31- - 使用闭包需要注意循环引用问题,Swift使用weak或unowned关键字解决循环引用问题
32- - 如果您的项目` Deployment Target >= 8.0 ` 那么编译时会警告` 'UISearchDisplayController' was deprecated in iOS version 8.0 ` ,请删除掉CE_UISearchDisplayController.swift文件及可。
55+ 使用闭包需要注意循环引用问题,Swift使用weak或unowned关键字解决循环引用问题
3356
3457##### UIKit ` delegate ` and ` dataSource ` method
3558
@@ -121,7 +144,6 @@ collectionView
121144- UIKit/UIScrollView
122145- UIKit/UISearchBar
123146- UIKit/UISearchController
124- - UIKit/UISearchDisplayController
125147- UIKit/UISplitViewController
126148- UIKit/UITabBar
127149- UIKit/UITabBarController
@@ -154,15 +176,15 @@ textField.ce_addControlEvents(UIControlEvents.EditingChanged | UIControlEvents.E
154176```
155177
156178- UIControl
157- - UIButton
158- - UIDatePicker
159- - UIPageControl
160- - UIRefreshControl
161- - UISegmentedControl
162- - UISlider
163- - UIStepper
164- - UISwitch
165- - UITextField
179+ - UIButton
180+ - UIDatePicker
181+ - UIPageControl
182+ - UIRefreshControl
183+ - UISegmentedControl
184+ - UISlider
185+ - UIStepper
186+ - UISwitch
187+ - UITextField
166188- UIBarButtonItem
167189- UIGestureRecognizer
168190
@@ -184,3 +206,9 @@ deinit {
184206 ce_removeObserver ()
185207}
186208```
209+
210+ ### License
211+
212+ Centipede is released under the MIT license. See LICENSE for details.
213+
214+
0 commit comments