swift 纯代码tableView

cell 

class DCTableViewCell: UITableViewCell {
    
    lazy var dcLabel: UILabel = {
        let dcLabel = UILabel()
        dcLabel.text = "百度"
        dcLabel.backgroundColor = UIColor.greenColor()
        dcLabel.textAlignment = NSTextAlignment.Center
        return dcLabel
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        addSubview(dcLabel);
        dcLabel.frame = CGRectMake(0, 0, 100, 80)
        dcLabel.text = "赵大财博客";
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

VC

import UIKit

class ViewController: UIViewController {
    
    lazy var tableView : UITableView = {
        return UITableView()
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
}

// MARK:- 设置UI界面相关
extension ViewController {
    /// 设置UI界面
    func setupUI() {
        // 0.将tableView添加到控制器的View中
        view.addSubview(tableView)
        
        // 1.设置tableView的frame
        tableView.frame = view.bounds
        
        // 2.设置数据源
        tableView.dataSource = self
        
        // 3.设置代理
        tableView.delegate = self
        
        //注册CELL
        tableView.registerClass(DCTableViewCell.self, forCellReuseIdentifier:"celli")
        
        
    }
}


// MARK:- tableView的数据源和代理方法
// extension类似OC的category,也是只能扩充方法,不能扩充属性
extension ViewController : UITableViewDataSource, UITableViewDelegate{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("celli");
        
        
        return cell!
        
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("点击了:\(indexPath.row)")
    }
    
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 80
    }
}

 

转载于:https://my.oschina.net/zhaodacai/blog/725002

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值