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 天前
继承、Lambda、Objective-C和Swift
开发语言·macos·ios·objective-c·swift·继承·lambda
KWMax2 天前
RxSwift系列(二)操作符
ios·swift·rxswift
Mamong2 天前
Swift并发笔记
开发语言·ios·swift
小溪彼岸2 天前
【iOS小组件】小组件尺寸及类型适配
swiftui·swift
Adam.com3 天前
#Swift :回调地狱 的解决 —— 通过 task/await 来替代 nested mutiple trailing closure 来进行 回调的解耦
开发语言·swift
Anakki3 天前
【Swift官方文档】7.Swift集合类型
运维·服务器·swift
KeithTsui3 天前
集合论(ZFC)之 联合公理(Axiom of Union)注解
开发语言·其他·算法·binder·swift
東三城3 天前
【ios】---swift开发从入门到放弃
ios·swift
文件夹__iOS7 天前
[SwiftUI 开发] @dynamicCallable 与 callAsFunction:将类型实例作为函数调用
ios·swiftui·swift