前言

成都創(chuàng)新互聯公司專注于開平網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供開平營銷型網站建設,開平網站制作、開平網頁設計、開平網站官網定制、小程序定制開發(fā)服務,打造開平網絡公司原創(chuàng)品牌,更為您提供開平網站排名全網營銷落地服務。
TableView 是 iOS 應用程序中非常通用的組件,幾乎每一個界面都有一個TableView,而我們許多的代碼都和TableView有關系,比如數據展示、更新TableView,一些響應選擇事件等,而這些大多都會通過其代理函數來實現,所以在VC中我們通常需要實現大量TableView的代理函數,如下面這樣
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 12.0
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.01
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}如果上面的代碼在每個VC中都實現一次,不僅寫了很多的重復的代碼,還增加了VC的復雜度,所以我在想能不能有一個統一的代理類,我們的TableView只要遵循它,就不用每次都要寫一大堆的代理方法,下面就是我寫的一個代理類的使用
示例代碼
private var delegate = CCDataSource()
lazy private var tableView: UITableView = {
let table = UITableView(frame: self.view.bounds, style: .grouped)
// 1.注冊cell
table.register(Custom1Cell.self, forCellReuseIdentifier: "cell1")
table.register(Custom2Cell.self, forCellReuseIdentifier: "cell2")
// 2.代理
table.delegate = self.delegate
table.dataSource = self.delegate
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(tableView)
self.setupTableView()
self.loadData()
}
private func loadData() {
// 3.網絡請求數據源,并賦值
delegate.datas = [[Model1(),Model1(),Model1()],[Model2(),Model2(),Model2(),Model2()]]
// 4.刷新視圖
tableView.reloadData()
}
private func setupTableView() {
// 在這里實現TableView的代理
delegate.identifier { (indexPath) -> (String) in
// 5.確定cell的類型
return indexPath.section == 0 ? "cell1" : "cell2"
}.headerHeight { (section) -> (CGFloat) in
// 6.頭部高度
return 12.0
}.footerHeight { (section) -> (CGFloat) in
// 7.尾部高度
return 0.01
}.rowHeight{ (indexPath, data) -> (CGFloat) in
// 8.行高
if let model = data as? Model1 {
return model.height()
}
if let model = data as? Model2 {
return model.height()
}
return 44.0
}.setContentCell { (cell, data) -> (Void) in
// 9.配置數據源
if let item = cell as? Custom1Cell, let model = data as? Model1 {
item.textLabel?.text = "Custom1Cell" + model.description
}
if let item = cell as? Custom2Cell, let model = data as? Model2 {
item.textLabel?.text = "Custom2Cell" + model.description
}
}.selected {[weak self] (indexPath, data) -> (Void) in
// 10.點擊事件(這里[weak self]需要防止循環(huán)引用)
self?.navigationController?.pushViewController(ViewController(), animated: true)
}
}上面這些步驟也不是固定的,這里有鏈式編程的思想,有些屬性可以不設置則會取默認值,當然也可以重復設置,不過此時后面的會覆蓋前面的
通過上面的方法,我們只需要創(chuàng)建一個CCDataSource實例,就可以在一個方法中將所有的TableView代理實現,而且在第5步時,我們就將cell與data對應起來了,后面會減少很多復雜的if else判斷,這不僅減少了代碼量,同時也使實現邏輯更加清晰
Demo地址:https://github.com/cdcyd/CCDataSource (本地下載)
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯的支持。
本文標題:iOS中TableView如何統一數據源代理詳解
鏈接URL:http://chinadenli.net/article16/ggpcdg.html
成都網站建設公司_創(chuàng)新互聯,為您提供網站排名、軟件開發(fā)、關鍵詞優(yōu)化、網頁設計公司、網站設計、網站收錄
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯