Swift使用纯代码自定义TableViewCell
2019-08-12 本文已影响10人
swift_honor
创建控件
lazy var labelTop = { () -> UILabel in
let label = UILabel.init()
//label.textColor = TYBaseTools.UIColorFromRGB(rgbValue: 0x333333, alpha: 1)
label.text = "订单信息"
label.font = UIFont.systemFont(ofSize: 25)
return label
}()
将控件添加到cell上
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addUI()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
func addUI() {
self.addSubview(self.labelTop)
}
在layoutSubviews()
方法中添加约束条件
override func layoutSubviews() {
super.layoutSubviews()
self.labelTop.snp.makeConstraints { (make) in
make.top.leading.equalTo(self).offset(16)
make.height.equalTo(26)
}
}
完整代码如下
//
// Cell.swift
// Alamofire
//
// Created by xx on 2019/8/12.
//
import UIKit
class EntruckingCell: UITableViewCell {
lazy var labelTop = { () -> UILabel in
let label = UILabel.init()
label.textColor = TYBaseTools.UIColorFromRGB(rgbValue: 0x333333, alpha: 1)
label.text = "订单信息"
label.font = UIFont.systemFont(ofSize: 25)
return label
}()
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addUI()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
func addUI() {
self.addSubview(self.labelTop)
}
///约束条件添加
override func layoutSubviews() {
super.layoutSubviews()
self.labelTop.snp.makeConstraints { (make) in
make.top.leading.equalTo(self).offset(16)
make.height.equalTo(26)
}
}
///数据填充
func fillViewWithData(data:Any) {
}
}