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 天前
Swift异步详解
swift
HarderCoder1 天前
强制 SwiftUI 重新渲染:`.id()` 这把“重启键”你用对了吗?
swift
HarderCoder1 天前
Swift 6.2 新语法糖:在字符串插值里直接给 Optional 写默认值
swift
HarderCoder1 天前
窥探 `@Observable` 的“小黑盒”:private 属性到底会不会被观察?
swift
zzywxc7871 天前
AI 在金融、医疗、教育、制造业等领域有着广泛的应用,以下是这些领域的一些落地案例
人工智能·python·spring cloud·金融·swift·空间计算
HarderCoder1 天前
Swift 并发避坑指南:自己动手实现“原子”属性与集合
swift
HarderCoder2 天前
惊!只是 `import Foundation`,`String.contains("")` 的返回值居然变了?
swift
HarderCoder2 天前
Swift 6.2 新武器:`weak let` —— 既弱引用又不可变的安全魔法
swift
HarderCoder2 天前
吃透 Swift 的 `@autoclosure`:把“表达式”变“闭包”的延迟利器
swift
HarderCoder2 天前
@Observable 遇上属性包装器:一键绕过‘计算属性’禁令的 Swift 5.9 实战技巧
swift