1.Swift基础控件:TableView列表

Swift TalbeView列表的使用

一、简介

在 iOS 开发中,UITableView 是一个常用的界面组件,用于显示列表型数据。UITableView 可以展示大量数据,并支持滚动、分组、选择等功能,是开发 iOS 应用中常见的组件之一。

以下是 UITableView 的一些重要特点和功能:

  1. 分组显示:UITableView 可以按照分组的方式显示数据,每个分组可以有一个 header 和一个 footer。

  2. 可定制的单元格:UITableView 中的每个单元格可以自定义样式,包括文本、图像、按钮等。

  3. 滚动和重用机制:UITableView 支持滚动功能,并且具有重用机制,可以重复使用单元格,提高性能。

  4. 数据源和委托:UITableView 需要通过数据源(UITableViewDataSource)和委托(UITableViewDelegate)来提供数据和处理用户操作。

  5. 编辑模式:UITableView 支持编辑模式,可以添加、删除、移动单元格等操作。

  6. 搜索功能:UITableView 支持搜索功能,可以根据关键字搜索数据。

在使用 UITableView 时,通常需要实现以下几个方法:

  • numberOfSections(in:):返回表格中的分组数量。
  • tableView(_:numberOfRowsInSection:):返回每个分组中的行数。
  • tableView(_:cellForRowAt:):返回指定单元格的内容。
  • tableView(_:didSelectRowAt:):处理用户点击单元格的操作。

除了以上方法外,还可以实现其他委托方法来自定义 UITableView 的外观和行为,比如设置 section header 和 footer、自定义单元格高度、处理单元格编辑等。

总的来说,UITableView 是 iOS 开发中非常常用的界面组件,可以用于展示各种类型的列表数据,提供了丰富的功能和定制选项,使得开发者能够轻松构建复杂的界面。

二、完整代码

import UIKit

class BaseTableViewController: ViewController,UITableViewDelegate, UITableViewDataSource {
   
    var tableView: UITableView!

    var arrTitle: NSArray!
    var arrList: NSMutableArray!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initData()
        
        tableView = UITableView(frame: self.view.bounds, style: UITableView.Style.grouped)
        tableView.delegate = self
        tableView.dataSource = self
//        tableView.tableHeaderView = tableViewHeader()
//        tableView.tableFooterView = tableViewFooter()
        self.view.addSubview(tableView)
      
    }
    
    /*初始化列表数据,在子类需要重写此方法*/
    func initData() {
        arrList = NSMutableArray.init()
        arrList = ["Item1", "Item 2", "Item 3"]

    }

    // MARK: - 列表tableView-header

    func tableViewHeader() -> UIView{
        let label:UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 80))
        label.text = "我是TableView的头部"
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 16)
        label.backgroundColor = UIColor.red
        label.textAlignment = NSTextAlignment.center
        return label
    }

    // MARK: - 列表tableView-footer

    func tableViewFooter() -> UIView{
        let label:UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 80))
        label.text = "我是TableView的尾部"
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 16)
        label.backgroundColor = UIColor.red
        label.textAlignment = NSTextAlignment.center
        return label
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "CELL"
        let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: identifier)
        cell.textLabel?.text = arrList[indexPath.row] as? String
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        NSLog("我被点击了%ld",indexPath.row)
    }
    
    // MARK: - 列表section-header
    
//    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//        return 40
//    }
//
//    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        let headerView = UIView()
//        headerView.backgroundColor = UIColor.lightGray
//
//        let headerLabel = UILabel(frame: CGRect(x: 15, y: 5, width: tableView.frame.width - 30, height: 30))
//        headerLabel.text = "Section \(section + 1) Header"
//        headerView.addSubview(headerLabel)
//
//        return headerView
//    }
    
    // MARK: - 列表section-footer

//    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        return 40
//    }
//
//    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//        let footerView = UIView()
//        footerView.backgroundColor = UIColor.lightGray
//
//        let footerLabel = UILabel(frame: CGRect(x: 15, y: 5, width: tableView.frame.width - 30, height: 30))
//        footerLabel.text = "Section \(section + 1) Footer"
//        footerView.addSubview(footerLabel)
//
//        return footerView
//    }
    
}

相关推荐

  1. 1.Swift基础TableView列表

    2024-04-05 17:52:05       36 阅读
  2. SwiftTableView的原理

    2024-04-05 17:52:05       28 阅读
  3. SwiftTableView的使用

    2024-04-05 17:52:05       35 阅读
  4. SwiftTableView的编辑模式

    2024-04-05 17:52:05       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 17:52:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 17:52:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 17:52:05       82 阅读
  4. Python语言-面向对象

    2024-04-05 17:52:05       91 阅读

热门阅读

  1. Python数据分析与挖掘

    2024-04-05 17:52:05       41 阅读
  2. Linux C++ 019-多态

    2024-04-05 17:52:05       29 阅读
  3. 在国企特定的环境中,如何激励低效能员工?

    2024-04-05 17:52:05       35 阅读
  4. 梦想编码:0基础解锁IT世界的无限可能

    2024-04-05 17:52:05       37 阅读
  5. 密码学基础古典密码

    2024-04-05 17:52:05       37 阅读
  6. 用O(1)时间复杂度实现bitset()函数

    2024-04-05 17:52:05       36 阅读
  7. ChatGPT:学术论文写作的秘密武器

    2024-04-05 17:52:05       42 阅读
  8. 蓝桥杯B组C++省赛——飞机降落(DFS)

    2024-04-05 17:52:05       29 阅读
  9. 算法刷题day39:树形DP

    2024-04-05 17:52:05       25 阅读
  10. 安卓手机APP开发的安卓工作台的简介

    2024-04-05 17:52:05       36 阅读