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
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值