最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS開發(fā)學(xué)習(xí)TableView展現(xiàn)一個(gè)list實(shí)例

 更新時(shí)間:2022年11月22日 09:18:30   作者:圣騎士Wind  
這篇文章主要為大家介紹了iOS系列學(xué)習(xí)TableView展現(xiàn)一個(gè)list實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

TableView 基礎(chǔ)

本文講講TableView的基本使用. 順便介紹一下delegation.

TableView用來做什么

TableView用來展示一個(gè)很長的list. 和Android中的RecyclerView不同, iOS中的TableView只能是豎直方向的list.

如何寫一個(gè)最簡單的TableView

一個(gè)最簡單的TableViewController看起來像這樣:

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ù).

這么簡單是因?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類, 然后自己動手做上面的幾條設(shè)置.

Delegate & DataSource

TableView有兩個(gè)重要的方面需要關(guān)注:

  • UITableViewDelegate: 管理和用戶的交互, 比如選擇, 滑動手勢等. 沒有必須要實(shí)現(xiàn)的方法.
  • UITableViewDataSource: 提供和管理數(shù)據(jù), 包括了數(shù)據(jù)對應(yīng)的cell或者h(yuǎn)eader. 有兩個(gè)必須要實(shí)現(xiàn)的方法(如上面的代碼例子所示).

繼承UIViewController

繼承UIViewController而不是UITableViewController之后, 需要自己寫一個(gè)tableView并加在view里. 再分別實(shí)現(xiàn)UITableViewDelegateUITableViewDataSource, 這里寫在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注冊這個(gè)Cell類型:

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
}

補(bǔ)充知識: Delegation

上面的方法初看可能會非常怪. 這里還涉及到了一個(gè)知識點(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ù)是被代理的對象.

References

Filling a table with data

Table View Guide

以上就是iOS系列學(xué)習(xí)TableView展現(xiàn)一個(gè)list實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于iOS TableView展現(xiàn)list的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

乐陵市| 恩平市| 楚雄市| 法库县| 枣庄市| 博湖县| 望都县| 高台县| 旬阳县| 弋阳县| 社旗县| 安新县| 灵山县| 尤溪县| 北票市| 班戈县| 石台县| 务川| 南漳县| 河曲县| 玉门市| 宣城市| 腾冲县| 汤原县| 公安县| 鄂伦春自治旗| 云和县| 黎平县| 乐清市| 施甸县| 吉隆县| 鄂伦春自治旗| 论坛| 华蓥市| 西乡县| 女性| 安徽省| 慈溪市| 宁南县| 从江县| 资阳市|