首页 > 其他分享 >[Android开发学iOS系列] TableView展现一个list

[Android开发学iOS系列] TableView展现一个list

时间:2022-11-22 00:23:08浏览次数:57  
标签:TableView tableView list iOS cell func data view

TableView 基础

本文讲讲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是想展示的数据类型, 可以hardcode一些数据.

这么简单是因为这个ViewController继承了UITableViewController, 并且cell的部分使用了storyboard.

这里需要用dequeueReusableCell方法, 是为了cell的复用, 因为list内容很多的时候cell view是可以循环使用的. (很像Android里的RecyclerView).

UITableViewController的签名是这样:

open class UITableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {

它为我们做了以下三件事:

  • 设置view为一个UITableView.
  • 设置delegate=self.
  • 设置dataSource=self.

这种方式的局限性在于第一点, 它的根view是一个TableView, 如果我们的需求比较复杂, 不仅仅是一个demo, 那么可能需要组合View.

拆解版TableView

我们也可以直接继承UIViewController类, 然后自己动手做上面的几条设置.

Delegate & DataSource

TableView有两个重要的方面需要关注:

  • UITableViewDelegate: 管理和用户的交互, 比如选择, 滑动手势等. 没有必须要实现的方法.
  • UITableViewDataSource: 提供和管理数据, 包括了数据对应的cell或者header. 有两个必须要实现的方法(如上面的代码例子所示).

继承UIViewController

继承UIViewController而不是UITableViewController之后, 需要自己写一个tableView并加在view里.
再分别实现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的几个原则:

  • 方法名以被代理的类型开头.
  • 方法的第一个参数是被代理的对象.

References

标签:TableView,tableView,list,iOS,cell,func,data,view
From: https://www.cnblogs.com/mengdd/p/iOS-tableview-basics.html

相关文章

  • CF1747E List Generation
    考虑将问题抽象成:左上角为\((0,0)\)右下角为\((n,m)\)的网格图,求所有满足至少有一条只向下或向右走的路径经过点集内所有点的的不同的点集大小之和。那么显然拐点有......
  • Playlist for Polycarp (hard version)
    本题显然只需要知道\(typ=1/2/3\)的歌的数量分别为什么就可以求出答案了。先随便求一下\(f_{i,j,k}\)表示取\(i\)个\(1\),\(j\)个\(2\),\(k\)个\(3\)的贡献......
  • vue+axios+ssm解决跨域问题
    环境1.vue-admin-template模板2.axios3.后端java跨域问题解决方案注意:配置了前端解决方案后端可以不用配置,反之后端配置了前端可以不用配置前端解决方案前端采用......
  • vue+axios跨域问题
    环境1.vue-admin-template模板2.axios3.后端java跨域问题解决方案前端vue.config.js文件在module.exports中找到devServer完成如下配置devServer:{port:p......
  • 微信小程序ios保存文件预览打不开的问题
    需求:微信小程序能下载xls文件到手机容易出错的点:1、没有配置域名,因为我的文件地址是阿里云的,小程序下载的时候会报没有配置域名,(因为我本地跑的时候勾选了不效验域名,所以......
  • C++初阶(list容器+模拟实现)
    list介绍list的本质是一个带头的双向循环链表。链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列......
  • axios二次封装-main.js使用
    axios封装:配置代理:axios封装:axios的二次封装:请求拦截统一token处理:引入:使用:导入:响应拦截统一错误处理:导入:main.js:login页面:效果:请求:我们要请求......
  • ArrayList和LinkedList的区别和使用场景
    先看看接口关系1.ArrayList和LinkedList区别ArrayList是对象数组实现的,LinkedListed是基于双链表实现的。LinkedList的存储消耗较大,除了存储数据外还要有上下节点的......
  • Java 比较两个对象的不同之处(old, new) 包含 bean 对象下的 list, Map , bean 的细节
    Java 比较两个对象的不同之处(old,new)  包含bean对象下的list,Map,bean的细节 packagecom.icil.pinpal.test1;importcom.alibaba.fastjson.JSONObject;......
  • ArrayList remove删除元素 为什么会出现ConcurrentModificationException异常
    ArrayListremove删除元素先看代码ArrayList<String>list=newArrayList<>();list.add("Java"); list.add("Python");for(Stringitem:list)......