IOS的UITableView控件简单使用

简介: IOS的UITableView控件简单使用

在IOS组件中,UITableView是几乎每个应用都会使用到的控件,没有之一。

UITableView简单使用

var arr : [String]?
 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.arr = ["1","2","3"]
        let tableView = UITableView(frame: self.view.bounds,style: UITableView.Style.plain)
        tableView.dataSource = self
        tableView.delegate = self;
        tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "qingcheng")
        self.view.addSubview(tableView)
    }

然后实现 UITableViewDelegate,UITableViewDataSource 两个协议。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("执行总条数")
        return arr!.count;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "qingcheng",for: indexPath)
        cell.textLabel?.text = "test"
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100;
    }

点击运行

自定义cell

复杂一点的应用都会自定义一个cell来满足自己的设计需求

使用xib来实现

创建一个cocoa touch class,勾选xib

在ViewDidLoad的时候,可以需要使用UINib来加载Xib

let tableView = UITableView(frame: self.view.bounds,style: UITableView.Style.plain)
        tableView.dataSource = self
        tableView.delegate = self;
        tableView.register(UINib.init(nibName: "MyCellTableViewCell", bundle: nil), forCellReuseIdentifier: "qingcheng")
        self.view.addSubview(tableView)

协议实现加载cell的方法也需要做对应的调整

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MyCellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "qingcheng",for: indexPath) as! MyCellTableViewCell
        cell.setName(name: "青城同学")
        return cell
    }

然后点击运行。

简单的使用差不多就到这里了。UITableView是一个非常强大的控件,比如删除cell,cell排序,机会都可以几行代码实现。

目录
相关文章
|
2月前
|
监控 iOS开发
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
26 0
|
10月前
|
iOS开发
iOS 多个滚动控件嵌套Demo
iOS 多个滚动控件嵌套Demo
51 0
|
10月前
|
iOS开发
iOS 常用的 上下左右 拉刷新控件
iOS 常用的 上下左右 拉刷新控件
62 0
|
iOS开发
iOS短信验证码控件,自动输入回调两次解决办法
iOS短信验证码控件,自动输入回调两次解决办法
375 0
|
缓存 算法 测试技术
iOS UITableView性能优化
iOS UITableView性能优化
iOS UITableView性能优化
|
iOS开发
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
136 0
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
|
安全 iOS开发
iOS小技能:下拉刷新控件的适配
1. 下拉顶部背景色设置: 往tableView的父控件添加拉伸背景视图 2. present 半屏适配 iOS13 modalPresentationStyle属性默认不是全屏样式`UIModalPresentationFullScreen`,而是半屏样式,需要根据需求手动设置。 present 半屏,会导致列表下拉刷新失效。
176 0
iOS小技能:下拉刷新控件的适配
|
iOS开发
iOS小技能:自动布局实现兄弟控件N等分且宽高比例是1:N(xib 上实现)
本文为 iOS视图约束专题的第三篇:xib上使用自动布局教程
154 0
|
iOS开发
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
294 0
|
iOS开发
iOS开发-关于UITableView去掉粘性的问题
iOS开发-关于UITableView去掉粘性的问题
71 0