Swift_UITableView

UITableView

iOS中非常非常非常重要的控件之一:UITableView,隆重登场!当有大批量相同数据的时候,使用列表视图,是非常明智的选择。
这里是写了UITableView的内容显示,还写了UITableView的编辑,希望小伙伴们熟练掌握 

UITableView : UIScrollView : UIView : UIResponder : NSObject

UITableView的显示

添加表示图到页面上,在控制器的viewDidLoad方法中编写如下代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // 初始化tableView,并设置frame和style
    let tableView = UITableView(frame: view.bounds, style: UITableViewStyle.Grouped)

    // 设置代理和数据源
    tableView.delegate = self
    tableView.dataSource = self

    // 添加到视图上
    view.addSubview(tableView)

    // 注册cell
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

}
  1. 在Swift中,如果需要遵守的协议没有遵守的话会直接报错
  2. 在Swift中,如果必须实现的协议方法没有实现,会直接报错

当前类遵守UITableViewDelegate / UITableViewDataSource协议

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // ...
}



UITableViewDataSource协议中的方法:

设置分组数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 3
}
设置行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}
设置每行上显示的内容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    cell.textLabel?.text = "textLabel"
    cell.imageView?.image = UIImage(named: "image.png")

    return cell
}
设置头区文字
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "头部区域"
}
自定义尾区
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    // 添加自定义label,并返回
    let footerLabel = UILabel(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
    footerLabel.textAlignment = NSTextAlignment.Center
    footerLabel.font = UIFont.boldSystemFontOfSize(20.0)
    footerLabel.text = "尾部区域"
    return footerLabel
}
设置快速索引
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
    return ["A", "B", "C", "D", "E", "F", "G", "H", "..."]
}



UITableViewDelegate协议中的方法:

设置行高
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 55
}
设置尾区的高度
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40.0
}
点击某行cell的方法
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // 保证cell,点击后失去选中效果
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    print("section:\(indexPath.row) row:\(indexPath.row)")
}



UITableView的编辑

删除Cell
设置TableView可以编辑
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // 返回true代表可以编辑(默认也是可以编辑)
    return true
}
设置编辑的样式
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    // 设置为删除样式(默认也是删除)
    return UITableViewCellEditingStyle.Delete
}
处理编辑过程
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    // 如果当前是删除样式
    if editingStyle == .Delete {

        // 处理数据
        // ....

        // 更新页面
        tableview.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}
修改删除按钮文字
func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
    return "静静的去吧.."
}
移动Cell
设置是否可以移动
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // 返回true代表可以编辑(默认也是可以移动)
    return true
}
处理移动过程
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

    // 处理数据
    // ...

    // 更新页面
    tableview.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath)
}
限制禁止跨分区移动
func tableView(tableView: UITableView, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath) -> NSIndexPath {

    // 如果是当前分组,就返回目标区域,代表可以移动,否则返回原区域,代表不可以移动
    if sourceIndexPath.section == proposedDestinationIndexPath.section {
        return proposedDestinationIndexPath
    } else {
        return sourceIndexPath
    }
}

好,关于 Swift 封装 UITableView 和 UICollectionView 的问题,我可以给你一些基本的介绍。 首先,UITableView 和 UICollectionView 都是 iOS 平台中常用的视图控件,用于展示列表和网格等数据。在 Swift 中,我们可以通过继承 UITableView 和 UICollectionView 类来自定义我们自己的表格和网格视图。 下面是一个简单的示例代码,展示如何封装 UITableView: ```swift import UIKit class MyTableView: UITableView, UITableViewDataSource, UITableViewDelegate { var data: [String] = [] override init(frame: CGRect, style: UITableView.Style) { super.init(frame: frame, style: style) self.dataSource = self self.delegate = self // 注册单元格 self.register(UITableViewCell.self, forCellReuseIdentifier: "cell") } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } // UITableViewDataSource 协议方法 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.data.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = self.data[indexPath.row] return cell } // UITableViewDelegate 协议方法 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("选中了第 \(indexPath.row) 行") } } ``` 在这个示例代码中,我们自定义了一个名为 `MyTableView` 的类,继承自 `UITableView`。我们在 `init` 方法中设置了数据源和代理,并注册了一个单元格类型。在 `UITableViewDataSource` 和 `UITableViewDelegate` 协议方法中,我们实现了表格的行数、单元格内容和选中事件的处理。 类似地,我们也可以使用类似的方式封装 UICollectionView。需要注意的是,UICollectionViewDelegate 和 UICollectionViewDataSource 两个协议方法和 UITableView 中的函数名和实现方式略有不同,需要根据实际情况来进行调整。 希望这个简单的示例代码可以对你有所帮助。如果你有其他关于 Swift 的问题,欢迎随时提出!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值