iOS中TableView如何統(tǒng)一數(shù)據(jù)源代理詳解
前言
TableView 是 iOS 應(yīng)用程序中非常通用的組件,幾乎每一個(gè)界面都有一個(gè)TableView,而我們?cè)S多的代碼都和TableView有關(guān)系,比如數(shù)據(jù)展示、更新TableView,一些響應(yīng)選擇事件等,而這些大多都會(huì)通過其代理函數(shù)來實(shí)現(xiàn),所以在VC中我們通常需要實(shí)現(xiàn)大量TableView的代理函數(shù),如下面這樣
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) }
如果上面的代碼在每個(gè)VC中都實(shí)現(xiàn)一次,不僅寫了很多的重復(fù)的代碼,還增加了VC的復(fù)雜度,所以我在想能不能有一個(gè)統(tǒng)一的代理類,我們的TableView只要遵循它,就不用每次都要寫一大堆的代理方法,下面就是我寫的一個(gè)代理類的使用
示例代碼
private var delegate = CCDataSource() lazy private var tableView: UITableView = { let table = UITableView(frame: self.view.bounds, style: .grouped) // 1.注冊(cè)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.網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)源,并賦值 delegate.datas = [[Model1(),Model1(),Model1()],[Model2(),Model2(),Model2(),Model2()]] // 4.刷新視圖 tableView.reloadData() } private func setupTableView() { // 在這里實(shí)現(xiàn)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.配置數(shù)據(jù)源 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.點(diǎn)擊事件(這里[weak self]需要防止循環(huán)引用) self?.navigationController?.pushViewController(ViewController(), animated: true) } }
- 注冊(cè)cell:這一步很重要,這個(gè)代理類只支持這種方式加載cell,你在該界面有幾種cell,就需要注冊(cè)幾個(gè)cell類
- 代理: 將代理實(shí)例賦值給tableView的代理,這里我將dataSource和delegate統(tǒng)一為delegate了,并且如果有多個(gè)TableView,我們還可以創(chuàng)建多個(gè)代理實(shí)例與其一一對(duì)應(yīng)
- 網(wǎng)絡(luò)請(qǐng)求:這里是做網(wǎng)絡(luò)請(qǐng)求地方,并且將請(qǐng)求后的數(shù)據(jù)保存在代理類中
- 刷新視圖
- 確定cell的類型:cell是通過它注冊(cè)identifier來創(chuàng)建的,所以根據(jù)indexPath來返回相應(yīng)的cell注冊(cè)的identifier即可
- 頭部高度:header的高度,可以是定值,也可以根據(jù)section來動(dòng)態(tài)返回
- 尾部高度:footer的高度,可以是定值,也可以根據(jù)section來動(dòng)態(tài)返回
- 行高:這里的行高可以通過data來獲取,這樣利于做高度緩存,也可以通過indexPath來動(dòng)態(tài)返回
- 配置數(shù)據(jù)源:這里可以獲取的已經(jīng)初始化號(hào)的cell和其對(duì)應(yīng)的數(shù)據(jù)源,我們只需要將其賦值給cell即可
- 點(diǎn)擊事件
上面這些步驟也不是固定的,這里有鏈?zhǔn)骄幊痰乃枷?,有些屬性可以不設(shè)置則會(huì)取默認(rèn)值,當(dāng)然也可以重復(fù)設(shè)置,不過此時(shí)后面的會(huì)覆蓋前面的
通過上面的方法,我們只需要?jiǎng)?chuàng)建一個(gè)CCDataSource實(shí)例,就可以在一個(gè)方法中將所有的TableView代理實(shí)現(xiàn),而且在第5步時(shí),我們就將cell與data對(duì)應(yīng)起來了,后面會(huì)減少很多復(fù)雜的if else判斷,這不僅減少了代碼量,同時(shí)也使實(shí)現(xiàn)邏輯更加清晰
Demo地址:https://github.com/cdcyd/CCDataSource (本地下載)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
IOS登錄頁面動(dòng)畫、轉(zhuǎn)場(chǎng)動(dòng)畫開發(fā)詳解
本篇文章通過詳細(xì)的步驟給大家詳細(xì)講述了IOS登錄頁面動(dòng)畫、轉(zhuǎn)場(chǎng)動(dòng)畫開發(fā)的詳細(xì)教程,有興趣的朋友參考學(xué)習(xí)下。2018-01-01淺述iOS11 Xcode 9 按住command 單擊 恢復(fù)到從前(直接跳轉(zhuǎn)到定義)
這篇文章主要介紹了 iOS11 Xcode 9 按住command 單擊 恢復(fù)到從前(直接跳轉(zhuǎn)到定義)的相關(guān)資料,需要的朋友可以參考下2017-10-10解析iOS應(yīng)用開發(fā)中對(duì)設(shè)計(jì)模式中的抽象工廠模式的實(shí)現(xiàn)
這篇文章主要介紹了解析iOS應(yīng)用開發(fā)中對(duì)設(shè)計(jì)模式中的抽象工廠模式的實(shí)現(xiàn),示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03解決蘋果ios用js的Date()出現(xiàn)NaN的問題
下面小編就為大家分享一篇解決蘋果ios用js的Date()出現(xiàn)NaN的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03iOS中導(dǎo)航欄pop返回時(shí)出現(xiàn)黑塊問題的解決方法
在iOS開發(fā)的工作當(dāng)中,Push和Pop經(jīng)常用于界面之間的跳轉(zhuǎn)和返回。下面這篇文章主要給大家介紹了關(guān)于iOS中導(dǎo)航欄pop返回時(shí)出現(xiàn)黑塊問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10