Swift 教程之TableView使用03自定义Cell
2019-05-03 本文已影响56人
iCloudEnd
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
}
}