日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

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

作者:圣騎士Wind ? 更新時間: 2022-12-21 編程語言

TableView 基礎(chǔ)

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

TableView用來做什么

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

如何寫一個最簡單的TableView

一個最簡單的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ù).

這么簡單是因為這個ViewController繼承了UITableViewController, 并且cell的部分使用了storyboard.

這里需要用dequeueReusableCell方法, 是為了cell的復(fù)用, 因為list內(nèi)容很多的時候cell view是可以循環(huán)使用的. (很像Android里的RecyclerView).

UITableViewController的簽名是這樣:

open class UITableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {

它為我們做了以下三件事:

  • 設(shè)置view為一個UITableView.
  • 設(shè)置delegate=self.
  • 設(shè)置dataSource=self.

這種方式的局限性在于第一點, 它的根view是一個TableView, 如果我們的需求比較復(fù)雜, 不僅僅是一個demo, 那么可能需要組合View.

拆解版TableView

我們也可以直接繼承UIViewController類, 然后自己動手做上面的幾條設(shè)置.

Delegate & DataSource

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

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

繼承UIViewController

繼承UIViewController而不是UITableViewController之后, 需要自己寫一個tableView并加在view里. 再分別實現(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也改用代碼類, 寫一個這樣的類:

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注冊這個Cell類型:

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

補充知識: Delegation

上面的方法初看可能會非常怪. 這里還涉及到了一個知識點是iOS中的delegate. 它存在的意義是為了拓展本身類的功能.

Apple自己的很多API就用了delegate protocol, 比如UIApplicationDelegate, UITableViewDelegate. 如果我們想自己定義一個:

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的幾個原則:

  • 方法名以被代理的類型開頭.
  • 方法的第一個參數(shù)是被代理的對象.

References

Filling a table with data

Table View Guide

原文鏈接:https://juejin.cn/post/7168500107095048199

欄目分類
最近更新