Swift 教程之TableView使用03自定义Cell

Swift 教程之TableView使用03自定义Cell

之前系列课程

Swift 教程之TableView使用03自定义Cell

自定义Cell是开发swift app的基本功,只有掌握好自定义cell的技术,才能不惧甲方的任何表态需求。下面就是一个自定义cell基础过程。

1. 新建一个swift,命名为CardCell

代码如下

class CardCell:UITableViewCell {
    var cellData:CellData! {
        didSet{
            textLabel?.text = cellData.title
            imageView?.image = cellData.featureImage
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.backgroundColor = .yellow
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

其中有下面一句

var cellData:CellData! {
       didSet{
           textLabel?.text = cellData.title
           imageView?.image = cellData.featureImage
       }
   }

2. 修改tableview

修改register

 fileprivate func setupTableView() {
        tableView.register(CardCell.self, forCellReuseIdentifier: CELL_ID)
    }

修改row生成

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath) as!CardCell
        let section=sections[indexPath.section]
        let cellData=section.data[indexPath.row]
        cell.cellData=cellData
        r

完成

你可以在didSet函数里尽情玩耍

var cellData:CellData! {
       didSet{
           textLabel?.text = cellData.title
           imageView?.image = cellData.featureImage
       }
   }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 没有比大自然更好的去处 没有比图书馆更接近天堂的地方 它们都一样 一样的圣洁和清新 一样的包罗万象 有时候 我也会...
    林清萓阅读 3,008评论 0 4