iOS開發(fā)學(xué)習(xí)TableView展現(xiàn)一個(gè)list實(shí)例
TableView 基礎(chǔ)
本文講講TableView的基本使用. 順便介紹一下delegation.
TableView用來(lái)做什么
TableView用來(lái)展示一個(gè)很長(zhǎng)的list. 和Android中的RecyclerView不同, iOS中的TableView只能是豎直方向的list.
如何寫一個(gè)最簡(jiǎn)單的TableView
一個(gè)最簡(jiǎn)單的TableViewController看起來(lái)像這樣:
class ViewController: UITableViewController { var data: [String] = [] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. // loadData() print(data) } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { data.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) cell.textLabel?.text = data[indexPath.row] return cell } }
這里data是想展示的數(shù)據(jù)類型, 可以hardcode一些數(shù)據(jù).
這么簡(jiǎn)單是因?yàn)檫@個(gè)ViewController繼承了UITableViewController
, 并且cell的部分使用了storyboard.
這里需要用dequeueReusableCell
方法, 是為了cell的復(fù)用, 因?yàn)閘ist內(nèi)容很多的時(shí)候cell view是可以循環(huán)使用的. (很像Android里的RecyclerView).
UITableViewController
的簽名是這樣:
open class UITableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {
它為我們做了以下三件事:
- 設(shè)置view為一個(gè)
UITableView
. - 設(shè)置
delegate=self
. - 設(shè)置
dataSource=self
.
這種方式的局限性在于第一點(diǎn), 它的根view是一個(gè)TableView, 如果我們的需求比較復(fù)雜, 不僅僅是一個(gè)demo, 那么可能需要組合View.
拆解版TableView
我們也可以直接繼承UIViewController
類, 然后自己動(dòng)手做上面的幾條設(shè)置.
Delegate & DataSource
TableView有兩個(gè)重要的方面需要關(guān)注:
- UITableViewDelegate: 管理和用戶的交互, 比如選擇, 滑動(dòng)手勢(shì)等. 沒(méi)有必須要實(shí)現(xiàn)的方法.
- UITableViewDataSource: 提供和管理數(shù)據(jù), 包括了數(shù)據(jù)對(duì)應(yīng)的cell或者h(yuǎn)eader. 有兩個(gè)必須要實(shí)現(xiàn)的方法(如上面的代碼例子所示).
繼承UIViewController
繼承UIViewController而不是UITableViewController
之后, 需要自己寫一個(gè)tableView并加在view里. 再分別實(shí)現(xiàn)UITableViewDelegate
和UITableViewDataSource
, 這里寫在extension里, 拆分完之后set給tableView:
tableView.delegate = self tableView.dataSource = self
整體改造后代碼如下:
class ViewController: UIViewController { var data: [String] = ["Hello", "World"] private let tableView = UITableView() override func loadView() { view = UIView() view.addSubview(tableView) tableView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ tableView.topAnchor.constraint(equalTo: view.topAnchor), tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor), tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), ]) } override func viewDidLoad() { super.viewDidLoad() tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell") tableView.delegate = self tableView.dataSource = self } } extension ViewController: UITableViewDelegate {} extension ViewController: UITableViewDataSource { func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int { data.count } func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as? MyCell { cell.configure(with: data[indexPath.row]) return cell } return UITableViewCell() } }
自己的Cell class
這里Cell也改用代碼類, 寫一個(gè)這樣的類:
class MyCell: UITableViewCell { private let label = UILabel() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) contentView.addSubview(label) label.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ label.topAnchor.constraint(equalTo: contentView.topAnchor), label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), ]) } @available(*, unavailable) required init?(coder _: NSCoder) { fatalError("init(coder:) has not been implemented") } func configure(with data: String) { label.text = data } }
注意tableView注冊(cè)這個(gè)Cell類型:
override func viewDidLoad() { super.viewDidLoad() tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell") }
補(bǔ)充知識(shí): Delegation
上面的方法初看可能會(huì)非常怪. 這里還涉及到了一個(gè)知識(shí)點(diǎn)是iOS中的delegate. 它存在的意義是為了拓展本身類的功能.
Apple自己的很多API就用了delegate protocol, 比如UIApplicationDelegate
, UITableViewDelegate
. 如果我們想自己定義一個(gè):
protocol MyTypeDelegate: AnyObject { func myType(_ myType: MyType, shouldDoSomething argumentString: String) -> Bool func myType(_ myType: MyType, didAbortWithError error: Error) func myTypeDidFinish(_ myType: MyType) } class MyType { weak var delegate: MyTypeDelegate? }
定義delegation的幾個(gè)原則:
- 方法名以被代理的類型開頭.
- 方法的第一個(gè)參數(shù)是被代理的對(duì)象.
References
以上就是iOS系列學(xué)習(xí)TableView展現(xiàn)一個(gè)list實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于iOS TableView展現(xiàn)list的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- iOS開發(fā)TableView網(wǎng)絡(luò)請(qǐng)求及展示預(yù)加載實(shí)現(xiàn)示例
- ios開發(fā)UITableViewCell圖片加載優(yōu)化詳解
- iOS ScrollView嵌套tableView聯(lián)動(dòng)滾動(dòng)的思路與最佳實(shí)踐
- iOS優(yōu)化UITableViewCell高度計(jì)算的一些事兒
- iOS自定義UITableView實(shí)現(xiàn)不同系統(tǒng)下的左滑刪除功能詳解
- iOS11解決UITableView側(cè)滑刪除無(wú)限拉伸的方法
- ios UITableView 自定義右滑刪除的實(shí)現(xiàn)代碼
相關(guān)文章
iOS實(shí)現(xiàn)毛玻璃效果(無(wú)需要第三方)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)毛玻璃效果,無(wú)需要第三方,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05iOS App開發(fā)中UIViewController類的使用教程
UIViewController是iOS中控制視圖的關(guān)鍵所在,這里我們將針對(duì)UIViewController的聲明周期與主要屬性和方法,來(lái)總結(jié)iOS App開發(fā)中UIViewController類的使用教程2016-07-07iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的方法
這篇文章主要給大家介紹了關(guān)于iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10Objective-C之Category實(shí)現(xiàn)分類示例詳解
這篇文章主要為大家介紹了Objective-C之Category實(shí)現(xiàn)分類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06iOS 實(shí)現(xiàn)簡(jiǎn)單的加載等待動(dòng)畫示例(思路與實(shí)現(xiàn))
本篇文章主要介紹了iOS 實(shí)現(xiàn)簡(jiǎn)單的加載等待動(dòng)畫示例(思路與實(shí)現(xiàn)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05