//
// BlogTableViewCell.swift
// App
//
// Created by kila on 16/02/2018.
// Copyright © 2018 kila. All rights reserved.
//
import UIKit
// 自定义cell类
class MyTableViewCell: UITableViewCell {
var uImgAvatar: UIImageView!
var uTxtContent: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
if !self.isEqual(nil) {
let uItemBox = UIView() // 第1步:为“Cell的contentView”创建一个子视图,用来容纳所有自定义的视图(这是关键中的关键)
uImgAvatar = UIImageView()
uTxtContent = UILabel()
self.contentView.addSubview(uItemBox)
uItemBox.addSubview(uImgAvatar)
uItemBox.addSubview(uTxtContent)
uItemBox.snp.makeConstraints { (make) -> Void in
make.edges.equalTo(self.contentView).inset(UIEdgeInsetsMake(0, 0, 0, 0)) // 第2步:设置uItemBox与“Cell的contentView”边缘对齐,可以有空隙,没必要一定是(0, 0, 0, 0)
}
// 第3步:uItemBox内部的子控件合理排版即可(由top和bottom与uItemBox的对齐基准决定了cell最终的高度)
uImgAvatar.snp.makeConstraints { (make) -> Void in
make.size.equalTo(CGSize(width:45, height:45))
make.top.equalTo(uItemBox).offset(25)
make.left.equalTo(uItemBox).offset(25)
}
uTxtContent.numberOfLines = 0
uTxtContent.snp.makeConstraints { (make) -> Void in
make.top.equalTo(uImgAvatar.snp.bottom).offset(0)
make.left.equalTo(uItemBox).offset(25)
make.right.equalTo(uItemBox).offset(-25)
make.bottom.equalTo(uItemBox).offset(-25)
}
}
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}