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
//    }
    
}
相关推荐
weixin_531670892 天前
Interlude起来:动作动画和文字通知哪个更适合休息提醒?Mac上有没有用桌宠做动作引导的软件?
人工智能·macos·mac·swift
Daniel_Coder2 天前
我用 SwiftUI + GRDB 做了一个「按时吃药·服药提醒」App
ios·swiftui·swift·widgetkit
末代iOS程序员华仔3 天前
iOS个人开发者账号申请为啥会被卡住
ios·swift·figma
denggun123454 天前
信号量(DispatchSemaphore vs AsyncSemaphore)、swift协作式线程池 and python信号量
开发语言·python·swift
2501_915918414 天前
Swift IDE 有哪些?按平台和需求选择Swift 开发工具
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
懋学的前端攻城狮5 天前
把 SwiftUI List 换成 NSTableView:一次目录树卡顿的排查记录
macos·swiftui·swift
2501_915106325 天前
Swift 开发环境搭建指南:三条路径按目标对号入座
开发语言·vscode·ios·objective-c·个人开发·swift·敏捷流程
承渊政道6 天前
【Python编程—从入门到实践】(Python操作列表深度教程:循环、切片、元组与代码风格)
开发语言·python·pycharm·列表·元组·切片
东坡肘子7 天前
AI 想放弃了,人没有 -- 肘子的 Swift 周报 #150
人工智能·swiftui·swift
冯汉栩7 天前
Swift Control RollingCountdownView(动画倒计时)
开发语言·ios·swift