{"id":19332316,"url":"https://github.com/klaus01/centipede","last_synced_at":"2026-02-26T18:32:22.453Z","repository":{"id":33102710,"uuid":"36740425","full_name":"klaus01/Centipede","owner":"klaus01","description":"Swift achieve a pure library closures achieve UIKit assembly delegate and dataSource methods.","archived":false,"fork":false,"pushed_at":"2016-10-27T12:27:08.000Z","size":465,"stargazers_count":28,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-22T16:58:30.152Z","etag":null,"topics":["delegate","extension","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/klaus01.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-02T14:53:28.000Z","updated_at":"2023-02-01T23:13:39.000Z","dependencies_parsed_at":"2022-07-12T20:20:28.625Z","dependency_job_id":null,"html_url":"https://github.com/klaus01/Centipede","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/klaus01/Centipede","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaus01%2FCentipede","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaus01%2FCentipede/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaus01%2FCentipede/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaus01%2FCentipede/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/klaus01","download_url":"https://codeload.github.com/klaus01/Centipede/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaus01%2FCentipede/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263789913,"owners_count":23511893,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["delegate","extension","swift"],"created_at":"2024-11-10T02:45:06.086Z","updated_at":"2026-02-26T18:32:22.386Z","avatar_url":"https://github.com/klaus01.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Centipede ![License MIT](https://img.shields.io/badge/license-MIT-green.svg?style=flat) ![CocoaPods](https://img.shields.io/cocoapods/v/Centipede.svg?style=flat) ![CocoaPods](http://img.shields.io/cocoapods/p/Centipede.svg?style=flat) ![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)\n\n一个 Swift 库，使用闭包实现 UIKit 等组件的 delegate 和 dataSource 方法\n\n# 使用\n\nXcode 8, Swift 3.0, iOS 8+\n\n所有方法名称以`ce_`开头\n\n```swift\n// UITableView\nlet kCellReuseIdentifier = \"MYCELL\"\ntableView.register(UITableViewCell.self, forCellReuseIdentifier: kCellReuseIdentifier)\ntableView.ce_numberOfSections_in { (tableView) -\u003e Int in\n    return 3\n}.ce_tableView_numberOfRowsInSection { (tableView, section) -\u003e Int in\n    return 5\n}.ce_tableView_cellForRowAt { (tableView, indexPath) -\u003e UITableViewCell in\n    return tableView.dequeueReusableCell(withIdentifier: kCellReuseIdentifier, for: indexPath)\n}.ce_tableView_willDisplay { (tableView, cell, indexPath) in\n    cell.textLabel?.text = \"\\(indexPath.section) - \\(indexPath.row)\"\n}.ce_scrollViewDidScroll { (scrollView) in\n    print(scrollView.contentOffset)\n}\n\n// UIControl\nbutton.ce_addControlEvents(.touchDown) { (control, touches) in\n    print(\"TouchDown\")\n}.ce_addControlEvents(.touchUpInside) { (control, touches) in\n    print(\"TouchUpInside\")\n}\nbutton.ce_removeControlEvents(.touchDown)\n\ntextField.ce_addControlEvents([.editingChanged, .editingDidBegin]) { (control, touches) in\n    print(\"TextChanged\")\n}\n\n// UIBarButtonItem\nlet barButtonItem = UIBarButtonItem()\nbarButtonItem.action { (barButtonItem) in\n    print(\"UIBarButtonItem action\")\n}\n\n// UIGestureRecognizer\nlet gestureRecognizer = UIPanGestureRecognizer { (gestureRecognizer) in\n    print(gestureRecognizer.state.rawValue)\n}\nself.view.addGestureRecognizer(gestureRecognizer)\n```\n\n#### 源码使用\n\n将`Centipede`目录复制到您的项目中及可。\n\n#### CocoaPods\n\n```\nplatform :ios, '8.0'\nuse_frameworks!\n\npod 'Centipede'\n```\n\n#### Carthage\n\n```\ngithub \"klaus01/Centipede\"\n```\n\n#### 注意\n\n使用闭包需要注意循环引用问题，Swift 使用 weak 或 unowned 解决循环引用问题\n\n# 原由\n\n在实现 delegate 的各个方法时：\n\n- 方法遍布整个 ViewController，散乱。\n- 具体的实现与成员变量被分开了，阅读时需要分开查看。\n- 如果 ViewController 中实现多个 UITableViewDataSource 时，方法中需要判断组件来做出反应。如下：（这很丑）\n\n```swift\nfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -\u003e Int {\n    return tableView == leftTableView ? leftDatas.count : rightDatas.count\n}\n```\n\n这些情况让代码不易阅读和维护。\n\n**希望：**\n\n- 代码连续。组件的构造、样式设置和各 delegate 实现方法可写在一个位置。\n- 独立。有多个 UITableView 时，tableViewA 和tableViewB 的 delegate 方法实现是独立的，互不干扰。\n\n# 实现列表\n\n### UIKit `delegate` and `dataSource` method\n\n- AVFoundation/AVAudioPlayer\n- AVFoundation/AVAudioRecorder\n- AVFoundation/AVSpeechSynthesizer\n- CoreBluetooth/CBCentralManager\n- CoreBluetooth/CBPeripheral\n- CoreBluetooth/CBPeripheralManager\n- CoreLocation/CLLocationManager\n- ExternalAccessory/EAAccessory\n- ExternalAccessory/EAWiFiUnconfiguredAccessoryBrowser\n- Foundation/NSKeyedArchiver\n- Foundation/NSKeyedUnarchiver\n- Foundation/NSMetadataQuery\n- Foundation/NSUserActivity\n- Foundation/XMLParser\n- GameKit/GKMatch\n- GLKit/GLKView\n- GLKit/GLKViewController\n- iAd/ADBannerView\n- iAd/ADInterstitialAd\n- MapKit/MKMapView\n- MediaPlayer/MPMediaPickerController\n- MediaPlayer/MPPlayableContentManager\n- MultipeerConnectivity/MCAdvertiserAssistant\n- MultipeerConnectivity/MCBrowserViewController\n- MultipeerConnectivity/MCNearbyServiceAdvertiser\n- MultipeerConnectivity/MCNearbyServiceBrowser\n- MultipeerConnectivity/MCSession\n- PassKit/PKAddPassesViewController\n- PassKit/PKPaymentAuthorizationViewController\n- PushKit/PKPushRegistry\n- QuartzCore/CAAnimation\n- QuartzCore/CALayer\n- QuickLook/QLPreviewController\n- SceneKit/SCNProgram\n- SpriteKit/SKScene\n- StoreKit/SKProductsRequest\n- StoreKit/SKRequest\n- StoreKit/SKStoreProductViewController\n- UIKit/NSLayoutManager\n- UIKit/NSTextStorage\n- UIKit/UIActionSheet\n- UIKit/UIAlertView\n- UIKit/UICollectionView\n- UIKit/UIDocumentInteractionController\n- UIKit/UIDocumentMenuViewController\n- UIKit/UIDocumentPickerViewController\n- UIKit/UIDynamicAnimator\n- UIKit/UIGestureRecognizer\n- UIKit/UIImagePickerController\n- UIKit/UINavigationBar\n- UIKit/UINavigationController\n- UIKit/UIPageViewController\n- UIKit/UIPickerView\n- UIKit/UIPopoverController\n- UIKit/UIPopoverPresentationController\n- UIKit/UIPresentationController\n- UIKit/UIPrinterPickerController\n- UIKit/UIPrintInteractionController\n- UIKit/UIScrollView\n- UIKit/UISearchBar\n- UIKit/UISearchController\n- UIKit/UISplitViewController\n- UIKit/UITabBar\n- UIKit/UITabBarController\n- UIKit/UITableView\n- UIKit/UITextField\n- UIKit/UITextView\n- UIKit/UIToolbar\n- UIKit/UIVideoEditorController\n- UIKit/UIViewController\n- UIKit/UIWebView\n\n### Other add target action method\n\n- UIControl\n    - UIButton\n    - UIDatePicker\n    - UIPageControl\n    - UIRefreshControl\n    - UISegmentedControl\n    - UISlider\n    - UIStepper\n    - UISwitch\n    - UITextField\n- UIBarButtonItem\n- UIGestureRecognizer\n\n# License\n\nCentipede is released under the MIT license. See LICENSE for details.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaus01%2Fcentipede","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklaus01%2Fcentipede","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaus01%2Fcentipede/lists"}