表格视图UITableView

(一)UITableView内部自动封装了一套复用机制。会让空闲的cell进入可重用线程池,当有新的cell出现会先去线程池中找有没有可复用的,没有才会创建。假如有100组数据,需要100个cell,但是手机上每屏只能放下10个,其实这时候只需创建11个cell就够用了。每一个数据模型就是一个cell。通过数据源方法来对每个cell进行数据设置。通过代理方法设置关于tableView的头,尾等视图设置。

(二)tableView有两种样式:UITableViewStylePlainUITableViewStyleGroup。一个是正常的,一个是分组的。

正常样式 和 分组样式

代码:

import UIKit

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: CELLID)
    }

    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            return 3
        } else if section == 1 {
            return 4
        }else {
            return 5
        }
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }
    
    private let CELLID = "CELLID"
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(CELLID, forIndexPath: indexPath)
        
        cell.textLabel?.text = "第\(indexPath.section)组第\(indexPath.row)行"
        
        return cell
        
    }

}

一个UITableView可以有多种不同类型的cell,可以通过cell的id来进行区分。

(三)系统自带的cell

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
    UITableViewCellStyleValue1,        // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
    UITableViewCellStyleValue2,        // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
    UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
}; 

其中包含主标签,副标签,图片和accessoryType按钮。

(四)设置UITableView的行高和头尾视图:初级的行高设置是在代理中完成了,那些复杂的高度不一的cell则需要另外设置。

    // cell行高设置
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 44
    }
    
    // footerView高度设置
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 50
    }
    
    // headerView高度设置
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    // 设置尾视图
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
        let v = UIView(frame: frame)
        v.backgroundColor = UIColor.yellowColor()
        return v
        
    }
    
    // 设置头视图
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
        let v = UIView(frame: frame)
        v.backgroundColor = UIColor.orangeColor()
        return v
    }

效果图:

(五)tableView的用户交互:

(1)当选中某行时会调用代理:

// 选中某航时调用
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
    }

(2)对cell进行增/删/移动操作会调用代理:

首先让tableView进入可编辑模式

tableView.editing = true
// 设置cell的编辑模式,有NONE,插入,删除三种
    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        
        return UITableViewCellEditingStyle.Insert
        
    }

此时就会出现编辑选项

(3)如果需要UITableViewCell支持移动,需要实现如下两个代理方法

    // 是否能够移动
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        
        return true
    }
    
    // 位置移动后回调的方法
    override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
        
        // 改变模型设置
    }

效果:

此时长按👉选项,就可以移动了。

(4)如需要删除某个cell。一来可以滑动删除,二来可以在上方所述的枚举中选的删除枚举并实现代理

代码:

    // 设置删除按钮的标题
    override func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
        return "删除"
    }
    
    // 删除/插入单击事件
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        // DOSOMETHING
        print("DOSOMETHING")
    }

效果:

(5)如果需要滑动删除,比较特殊,此时应该关闭tablview的交互并且实现代理

tableView.editing = false
    // 是否能够滑动
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    // 设置cell的编辑模式,有NONE,插入,删除三种
    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        
        return UITableViewCellEditingStyle.Delete
        
    }
    
    // 删除/插入单击事件
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        // DOSOMETHING
        print("DOSOMETHING")
        
        
    }

(6)为UITableView添加索引:类似于通讯录。右边会有一栏索引功能。当选择索引就会跳转到相应的区域,需要实现如下一个代理方法。

    // 索引功能
    override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
        
        return ["A","B","C","D","E","F"]
    }

效果如下:

当选择A,就会去第一组,B就回去第二组。 这可是做查询必不可少的一部哦。

posted @ 2016-09-01 15:42  三更小新  阅读(198)  评论(0编辑  收藏  举报