swift tableView簡單實用-01 自定義cell

系統提供的UITableViewCell很難滿足我們的需求,自定義cell是常有的事,所以簡單使用table後寫了個自定義cell,具體如下:

1、創建自定義cell,繼承UITableViewCell,開發語言選擇swift,如下圖所示:

2、定義之後,在引用的vc中,

1)在viewDidLoad中添加註冊,

table.register(NormalCell.self, forCellReuseIdentifier: "normalcell")

2)在代理方法cellForRowAt中,初始化cell,具體如下所示:

        let cell = (tableView.dequeueReusableCell(withIdentifier: "normalcell", for: indexPath))as! NormalCell
        
        cell.lbl.text = String(format: "行:%d", indexPath.row+1)//自定義cell中的元素
        cell.logo.image = UIImage.init(named: "salary_select")//自定義cell中的元素

3、在自定義cell中有兩個元素,一個是UILabel,一個是UIImageView,其中定義有兩種方式

方法一:

1)定義元素的時候,不設置默認值(即不初始化),代碼如下:

    var lbl:UILabel
    var logo:UIImageView

2)需要在複寫的init初始化方法中,初始化元素,然後調用父類初始化方法,具體如下圖:

方法二:

1)定義元素的時候,設置默認值(即初始化),代碼如下:

    var lbl = UILabel.init()
    var logo = UIImageView.init()

2)在複寫的init初始化方法中,可以先調用父類初始化方法,再設置元素,這點和oc有點不一樣,具體如下圖:

4、運行結果如下圖所示:

5、全部代碼如下:

ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init()
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 20)
        view.backgroundColor = UIColor.lightGray
        
        let lbl = UILabel.init()
        lbl.frame = CGRect(x: 15, y: 0, width: 100, height: 20)
        lbl.text = String(section)
        view.addSubview(lbl)
        
        return view
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "naormalCell")
//        cell.textLabel?.text = String(format: "行:%d", indexPath.row+1)//String(indexPath.row)
//        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        
        let cell = (tableView.dequeueReusableCell(withIdentifier: "normalcell", for: indexPath))as! NormalCell
        
        cell.lbl.text = String(format: "行:%d", indexPath.row+1)
        cell.logo.image = UIImage.init(named: "salary_select")
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let destination = SeconViewController()
        destination.message = "傳遞的字符串"
        self.present(destination, animated: true, completion: nil)
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return UITableViewCell.EditingStyle.delete
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCell.EditingStyle.delete {
            //刪除數據
            
        }
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "刪除"
    }
    
    lazy var table:UITableView = {
        let table = UITableView(frame: self.view.bounds,style: UITableView.Style.plain)
        table.backgroundColor = UIColor.lightGray
        return table
    }()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        self.view .addSubview(table)
        table.register(NormalCell.self, forCellReuseIdentifier: "normalcell")
        table.delegate = self
        table.dataSource = self
        
        
        // Do any additional setup after loading the view.
    }


}

NormalCell.swift

import UIKit

class NormalCell: UITableViewCell {
    
    var lbl = UILabel.init()
    var logo = UIImageView.init()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        lbl = UILabel.init(frame: CGRect.zero)
        lbl.frame = CGRect(x: 65, y: 10, width: UIScreen.main.bounds.size.width, height: 40)
        
        logo = UIImageView.init()
        logo.frame = CGRect(x: 15, y: 10, width:40, height: 40)
        logo.layer.cornerRadius = 5
        logo.layer.masksToBounds = true
        
        self.backgroundColor = UIColor.white
        self.addSubview(lbl)
        self.addSubview(logo)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    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
    }

}

這樣就可以實現自定義cell了

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章