Swift学习笔记二 TableView

闲话不多说

首先初始化一个tableView

    fileprivate var tableView :UITableView = {
        
        //和OC的代码其实区别不是很大
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width:mScreenWidth, height:mScreenHeight), style: .plain)   //.plain   .grouped 分组与不分组
        tableView.rowHeight = 50                   //设置row的高度
        tableView.backgroundColor = UIColor.green     //背景颜色
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false   //是否显示水平和垂直滚动条
        tableView.separatorStyle = .none
        tableView.register(UITableViewCell.self, forCellReuseIdentifier:tabelViewCellID)//注册tableViewCell
   
        return tableView;
        
    }()

可以看出和OC代码区别并不是很大,唯一的区别是delegate和dataSource的设置不能再此处设置 注册tableViewCell也是和OC一样,在之前现给Identitfier标识 let tabelViewCellID = "tableViewCell"

设置代理

    fileprivate func setupUI(){
        //设置tableView 的代理
        tableView.delegate = self
        tableView.dataSource = self
        //添加到View上
        self.view .addSubview(tableView)
        
    }

在viewDidLoad()方法里面设置代理并添加到self.view上,当然,我这里是ViewController,如果是TableViewController就另当别论了

    override func viewDidLoad() {
        super.viewDidLoad()
        //ui布局
        setupUI()
    
    }

然后是添加代理方法,因为我是ViewController,所以要手动添加

extension ViewController :UITableViewDataSource,UITableViewDelegate {
    
    
    //tableView Sections
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1;
    }
    
    
    //row
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 10;
    }
    
    //cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
 
        var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: tabelViewCellID, for: indexPath)
        
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: tabelViewCellID)
        }
        
        cell.textLabel?.text =  "\(indexPath.row)"
        
        return cell
    }

}

虽然样子改变了,但是熟悉OC代码的小伙伴应该也是一眼就能明白。

运行结果

这样简单的tableView就做好了。

下面是完整代码

import UIKit

let tabelViewCellID = "tableViewCell"

class ViewController: UIViewController {
    
    fileprivate var tableView :UITableView = {
        
        //和OC的代码其实区别不是很大
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width:mScreenWidth, height:mScreenHeight), style: .plain)   //.plain   .grouped 分组与不分组
        tableView.rowHeight = 50                   //设置row的高度
        tableView.backgroundColor = UIColor.green     //背景颜色
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false   //是否显示水平和垂直滚动条
        tableView.separatorStyle = .none
        tableView.register(UITableViewCell.self, forCellReuseIdentifier:tabelViewCellID)//注册tableViewCell
   
        return tableView;
        
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        //ui布局
        setupUI()
    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController{
    
    fileprivate func setupUI(){
        //设置tableView 的代理
        tableView.delegate = self
        tableView.dataSource = self
        //添加到View上
        self.view .addSubview(tableView)
        
    }
}


extension ViewController :UITableViewDataSource,UITableViewDelegate {
    
    
    //tableView Sections
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1;
    }
    
    
    //row
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 10;
    }
    
    //cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
 
        var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: tabelViewCellID, for: indexPath)
        
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: tabelViewCellID)
        }
        
        cell.textLabel?.text =  "\(indexPath.row)"
        
        return cell
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值