Swift学习记录 -- 15.tableView的创建与基本操作

算是正式接触到真正的东西了吧 , 大概写了下 , 不是太习惯 , 虽然有了extension , 和// MARK:- 分类注释 , 懒加载也不是太习惯 . 可能是OC敲太多了 , 还不是太适应吧 .


import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{

    //懒加载
    let tableView:UITableView = {

        let tableView = UITableView()
        tableView.rowHeight = 100
        tableView.estimatedRowHeight = 100

        tableView.separatorColor = UIColor.redColor()

        return tableView
    }()


    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
    }
}

// extension + 类名  扩展,将方法抽出封装 (跟OC分类有点像,只能扩充方法,不能扩充属性)

// MARK:- 初始化UI
extension ViewController {

    func initUI() {

        //设置frame
        tableView.frame = view.bounds
        //设置数据源
        tableView.dataSource = self
        //设置代理
        tableView.delegate = self
        //添加到view上
        view.addSubview(tableView)

    }
}

// MARK:- 代理方法
extension ViewController{

    //代理方法
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("当前选中行 - \(indexPath.row)")
    }
    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    }
}

// MARK:- 数据源方法
extension ViewController{

    //数据源方法
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 10
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cellID = "cell"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellID)
        if cell == nil {

            //枚举的使用 : 1.枚举类型 + '.' + 具体类型 2.'.'+具体类型
            //            cell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier: cellID)
            cell = UITableViewCell(style: .Default, reuseIdentifier: cellID)
        }

        cell?.textLabel?.text = "嘿嘿嘿-\(indexPath.row)"

        return cell!
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值