title: UITableView 使用 AutoLayout 實(shí)現(xiàn) Cell 高度自適應(yīng)
date: 2017-07-01 13:18:12
tags: [ios, UITableView]
category: ios
使用 SnapKit 實(shí)現(xiàn) AutoLayout
效果圖
UITableView 主要代碼
lazy var mTableView: UITableView = {
let tv = UITableView()
tv.separatorStyle = .none
tv.register(Cell.self, forCellReuseIdentifier: Cell.CELL_IDENTIFIER)
tv.estimatedRowHeight = 40
tv.rowHeight = UITableViewAutomaticDimension
return tv
}()
Cell 主要代碼
contentView.addSubview(mLabelTitle)
contentView.addSubview(mLabelContent)
contentView.addSubview(mLabelTime)
mLabelTitle.snp.makeConstraints { (make) in
make.top.equalTo(contentView).offset(4) // top 必須和 contentView 關(guān)聯(lián)挎挖,才能把 contentView 的高度撐開
make.left.equalTo(contentView).offset(16)
}
mLabelContent.numberOfLines = 0 // 如果不設(shè)置這個 content 就是一行人弓,不會展開
mLabelContent.snp.makeConstraints { (make) in
make.top.equalTo(mLabelTitle.snp.bottom).offset(3)
make.left.equalTo(contentView).offset(16)
make.right.equalTo(contentView).offset(-16)
}
mLabelTime.snp.makeConstraints { (make) in
make.top.equalTo(mLabelContent.snp.bottom).offset(3)
make.bottom.equalTo(contentView).offset(-4) // bottom 必須和 contentView 關(guān)聯(lián)米奸,才能把 contentView 的高度撐開
make.right.equalTo(contentView).offset(-16)
}
初學(xué) ios 添怔,歡迎指教