本来打算hard code 一个类,充当当前实现两个类的角色,KeyValueWithIndicatorCell 和 KeyValueWithoutIndicatorCell。但最终实现,还是选择xib自动布局来实现。具体原因如下:
1、hard code实现的话,仍然需要根据hasDetailIndicator这个布尔值,进行布局调整,这个调整在cell update时,而不是cell init时。
2、xib可视化全扫
本打算hard code去实现的code:
public final class HardCodeTestCell: UITableViewCell {
public var key: UILabel = UILabel()
public var value: UILabel = UILabel()
private var detailIndicator: UIImageView = UIImageView()
private var line: UIView = UIView()
public var hasDetailIndicator: Bool = true
public static let height: CGFloat = 44.0
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = UIColor.purple
line.backgroundColor = UIColor.black
contentView.addSubview(line)
line.snp.makeConstraints { (make) in
make.height.equalTo(0.5)
make.width.equalTo((line.superview?.snp.width)!)
make.top.equalToSuperview().offset(43.5)
make.bottom.equalToSuperview()
}
detailIndicator.image = UIImage()
detailIndicator.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.bottom.equalToSuperview().offset(-10)
make.width.equalTo(12)
}
if hasDetailIndicator {
detailIndicator.isHidden = false
} else {
detailIndicator.isHidden = true
}
key.backgroundColor = UIColor.red
key.font = UIFont.systemFont(ofSize: 14)
key.textAlignment = .left
key.text = "Fiona Yang"
key.textColor = UIColor.black
contentView.addSubview(key)
key.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(10)
make.left.equalToSuperview().offset(10)
make.bottom.equalToSuperview().offset(-10)
make.width.greaterThanOrEqualTo(95)
}
value.backgroundColor = UIColor.blue
value.font = UIFont.systemFont(ofSize: 14)
value.textAlignment = .right
value.text = "Fiona Yang"
value.textColor = UIColor.black
contentView.addSubview(value)
value.snp.makeConstraints { (make) in
//TODO: do I need to set offset equal to 10
make.top.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.bottom.equalToSuperview().offset(-10)
make.width.greaterThanOrEqualTo(194)
// make.trailing.greaterThanOrEqualTo(value.snp.leadingMargin).offset(5)
make.leading.greaterThanOrEqualTo(key.snp.trailingMargin).offset(5)
// if (hasDetailIndicator) {
//
// } else {
//
// }
}
}
public convenience init(_ hasDetailIndicator: Bool, reuseIdentifier: String?) {
//TODO: werid, should call init method after all custom properties, while compile error
self.init(style: .default, reuseIdentifier: reuseIdentifier)
self.hasDetailIndicator = hasDetailIndicator
key.snp.makeConstraints { (make) in
//TODO: do I need to set offset equal to 10
make.top.equalToSuperview()
make.leading.equalToSuperview()
make.bottom.equalToSuperview()
make.width.lessThanOrEqualTo(95)
make.trailing.equalTo(value.snp.leading).offset(5)
if (hasDetailIndicator) {
} else {
}
}
key.backgroundColor = UIColor.red
key.font = UIFont.systemFont(ofSize: 14)
key.textAlignment = .left
key.text = "Fiona Yang"
key.textColor = UIColor.black
contentView.addSubview(key)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
tableview调用:
tableView.register(HardCodeTestCell.self, forCellReuseIdentifier: "\(HardCodeTestCell.self)")
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: HardCodeTestCell? = tableView.dequeueReusableCell(withIdentifier: "HardCodeTestCell", for: indexPath) as? HardCodeTestCell
if cell == nil {
cell = HardCodeTestCell(true, reuseIdentifier: "HardCodeTestCell")
}
// Configure the cell...
return cell!
}
实现过程中,未弄懂的地方:
- 这行代码并未调用cell的public convenience init(_ hasDetailIndicator: Bool, reuseIdentifier: String?)方法?
- 不理解这几行代码:
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
- 当前cell中的public convenience init(_ hasDetailIndicator: Bool, reuseIdentifier: String?)方法,应该需要重新命名成fillData,从而更新布局,在
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
此方法中创建所有cell。