swift中UITableView的使用

import UIKit

class MainViewController: UIViewController {
    
    var tableVie: UITableView?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.red
        
        self.setupTableview()

    }//http://www.jianshu.com/p/f790123c4b6f
    
    func setupTableview() {
        let tableViewFrame = CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-64)
        self.tableVie = UITableView(frame: tableViewFrame, style: .plain)
        self.tableVie?.delegate = self
        self.tableVie?.dataSource = self
        self.view.addSubview(self.tableVie!)
        
        // 注册cell
        self.tableVie?.register(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
        // 清除多余的cell分割线
        self.tableVie?.tableFooterView = UIView()
        
    }

}

extension MainViewController: UITableViewDelegate, UITableViewDataSource {
    
    // MARK: UITableViewDataSourse
    // 可选方法
    // 分区
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    // MARK: UITableViewDataSourse
    // 必选方法
    // 返回表格行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    // MARK: UITableViewDataSource
    // 必选方法
    // 创建各个单元格先生内容,(创建参数indexPath制定单元)
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let identify:String = "SwiftCell"
        let cell = tableVie?.dequeueReusableCell(withIdentifier: identify, for: indexPath)
        // 通过register 注册cell,不用判断非空
        let label = UILabel(frame: CGRect(x: 10, y: 0, width: 44, height: 44))
        label.backgroundColor = UIColor.red
        cell?.contentView.addSubview(label)
        return cell!
    }
    // MARK: UITableViewDelegate方法,cell的点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableVie?.deselectRow(at: indexPath, animated: true)
        // 创建UIAlertController
        let alertControl = UIAlertController(title: "提示", message: "你选中了\(indexPath.row)", preferredStyle: .alert)
        // 创建UIAlertAction
        let okAction = UIAlertAction(title: "确定", style: .default, handler: nil)
        alertControl.addAction(okAction)
        // 将UIAlertController present 出来
        self.present(alertControl, animated: true, completion: nil)
    }
    // 返回cell的高度,默认高度是44
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    // 返回分区头部标题内容高度
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    // 返回分区尾部内容高度
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 50
    }
    // 返回分区头部标题
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "头标题"
    }
    // 返回分区脚标题
    func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        return "脚标题"
    }
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return ["1", "2", "3", "4", "5"]
    }
    // MARK: 滑动删除必须实现的方法
    // 如果没实现则无法策划
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        print("删除\(indexPath.row)")
    }
    // 侧滑删除
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.delete
    }
    // 设置侧滑的文字
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除"
    }
}


你可能感兴趣的:(swift中UITableView的使用)