Swift 系统内置UITableViewCell样式及动态使用Cell

引言:

系统内置的cell样式由UITableViewCell类提供(该类由 NSClassFromString反射而来),在原型模型或小型系统而言开发便捷性有很大帮助,使用时指定init方法的参数即可

defalut样式:

样式说明:
一个ImageView在cell最左边;紧贴着该控件的是一个显示文字的Label,当然也可以添加辅助视图(这里为UISwitch)
图示:

在这里插入图片描述

代码:

class DFViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
   
    
    var dataArray: Array<String> = ["C","C++","Java","Object-C","Swift", "C#"]
    var tableView: UITableView!
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   
        dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   
        var cell = tableView.dequeueReusableCell(withIdentifier: "INTEGRAL_2", for: indexPath)
        // cell样式
        cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "INTEGRAL_2")
        
        cell.textLabel?.text = dataArray[indexPath.row]
        cell.imageView?.image = UIImage(named: "apple")
        
        if (indexPath.row / 2) == 0 {
   
            cell.detailTextLabel?.text = "Strong Static"
        } else if (indexPath.row / 2) != 0{
   
            cell.detailTextLabel?.text = "Weak Dynamic"
        }
        
        cell.accessoryView = UISwitch()
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   
        let cell = tableView.dequeueReusableCell(withIdentifier: "INTEGRAL_2", for: indexPath)
        print("\(indexPath.row)")
        switch indexPath.row {
   
        case 2:
            self.dismiss(animated: true, completion: nil)
        default:
            break
        }
    }
    
    private func initView(){
   
        tableView = UITableView(frame: self.view.frame, style: .plain)
        tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "INTEGRAL_2")
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }

    override func viewDidLoad() {
   
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = UIColor.white
        
        initView()
        
    }

}

subtitle样式:

样式说明:
一个ImageView在cell最左边;紧贴着该控件的是一个显示主标题的Label,该Label下方还有一个字号较小的Label。其样式类似于微信

图示:
在这里插入图片描述

代码:

class SUBViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
   
    
    var dataArray: Array<String> = ["Bob","Jackson","Edward","Lucy","Nancy", "Pony", "Leo", "Alice"]
    var tableView: UITableView!
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值