Swift4 2 UITableView 基本用法

前言:iOS项目中使用UITableView可谓是非常频繁,下面以三种不同的方式简单介绍一下Swift当中TableView的使用 第一种方式: 继承自UIViewController创建控制器,然后初始化TableView,遵守其代理,实现其代理方法,根据需求设置行高行数,初始化一个系统样式cell,显示相应数据

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    var tableView:UITableView!
    var array:[String] = ["纯代码自定义cell", "nib自定义cell"]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "UITableIView小解"
        self.view.backgroundColor = UIColor.white
        tableView = UITableView(frame:CGRect(x:0, y:0, width:UIScreen.main.bounds.width, height:UIScreen.main.bounds.height))
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }
    // 每个分区行数(默认分区为一个)
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    // 行高
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60.0
    }
    // cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "CellIdentifier")
        cell.textLabel?.text = array[indexPath.row]
        return cell
    }
    // cell点击事件处理
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            let vc = CustomCellController()
            self.navigationController?.pushViewController(vc, animated: true)
            break
        case 1:
            let vc = NibCellController()
            self.navigationController?.pushViewController(vc, animated: true)
            break
        default:
            
            break
        }
    }
}

复制代码

第二种方式: 该种方式采用继承自UITableViewController,并采用纯代码方式自定义cell。(继承了UITableViewController,会帮助我们自动初始化,并遵守代理实现代理方法)

// 控制器中代码
import UIKit

class CustomCellController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "CustomCell"
        let cell = CustomTableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: identifier)
        cell.setValueForCell()
        return cell
    }
复制代码
// 自定义cell CustomTableViewCell中代码
import UIKit

class CustomTableViewCell: UITableViewCell {
    var iconImage     : UIImageView?
    var titleLabel    : UILabel?
    var subTitleLabel : UILabel?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    func setUpUI(){
        // 图片
        iconImage = UIImageView(frame: CGRect(x:10, y: 10, width:60, height: 60))
        self.addSubview(iconImage!)
        
        // 大标题
        titleLabel = UILabel(frame: CGRect(x:(iconImage?.frame.size.width)!+20, y:10, width: self.frame.size.width-(iconImage?.frame.size.width)!+20, height:30))
        titleLabel?.textColor = UIColor.red
        self.addSubview(titleLabel!)
        
        // 副标题
        subTitleLabel = UILabel(frame: CGRect(x:(iconImage?.frame.size.width)!+20, y:(titleLabel?.frame.size.height)!+20, width:self.frame.size.width-(iconImage?.frame.size.width)!+20, height: 30))
        subTitleLabel?.font = UIFont.systemFont(ofSize: 14)
        subTitleLabel?.textColor = UIColor.purple
        self.addSubview(subTitleLabel!)
    }
    
    // 给cell赋值,项目中一般使用model,我这里直接写死了
    func setValueForCell(){
        iconImage?.image = UIImage(named:"image")
        titleLabel?.text = "大大大大的标题"
        subTitleLabel?.text = "副副副副的标题"
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
复制代码

第三种方法: 该方法控制器继承自UIViewController,采用Nib方式自动以cell, 该方式需要注意的是控制器中注册的时候的Identifier应和nib中的相同

import UIKit

class NibCellController: UIViewController ,UITableViewDataSource, UITableViewDelegate{
    
    let identifier = "NibTableViewCell"
    var tableView : UITableView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        tableView = UITableView(frame:CGRect(x:0, y:0, width:UIScreen.main.bounds.width, height:UIScreen.main.bounds.height))
        tableView?.delegate = self
        tableView?.dataSource = self
        self.view.addSubview(tableView!)
        // 注册nib
        let nib = UINib.init(nibName: "NibTableViewCell", bundle: nil)
        tableView?.register(nib, forCellReuseIdentifier: identifier)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier)
        return cell!
    }
}
复制代码

github源码github.com/daomoer/YYS…

转载于:https://juejin.im/post/5c88e4eb5188257e8f617615

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值