在iOS中考蕾,我們可以使用自動(dòng)布局來(lái)定義表格視圖單元格的高度掖肋,但是甩苛,默認(rèn)情況下蹂楣,該功能未啟用。
通常讯蒲,單元格的高度由表視圖委托的tableView:heightForRowAtIndexPath:方法決定痊土。要啟用自定義表視圖單元格,必須將表視圖的rowHeight屬性設(shè)置為UITableViewAutomaticDimension墨林。還必須為estimateRowHeight屬性分配一個(gè)值赁酝。一旦設(shè)置了這兩個(gè)屬性犯祠,系統(tǒng)將使用自動(dòng)布局來(lái)計(jì)算行的實(shí)際高度。
tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableViewAutomaticDimension
接下來(lái)酌呆,在Cell的內(nèi)容視圖中布置Cell的內(nèi)容衡载。要定義單元格的高度,還需要一個(gè)完整的約束和視圖鏈(具有明確的高度)來(lái)填充內(nèi)容視圖的頂部邊緣和底部邊緣之間的區(qū)域肪笋。如果在Cell 中有固定的高度月劈,系統(tǒng)將使用這些值。如果不是藤乙,則須向視圖或內(nèi)容視圖本身添加適當(dāng)?shù)母叨燃s束猜揪。
UITableViewController
import UIKit
class FirstViewController: UITableViewController {
let cellIdentifier = "cellIdentifier"
let listData = [("title", "content"),
("Trump 'to do everything' for Middle East peace", "The only credible ideas still require the creation of an independent and sovereign Palestinian state alongside Israel. The reality is that the Israelis and Palestinians are way apart on the main issues - the future of east Jerusalem, the fate of Palestinian refugees and the borders of an independent Palestine. The two sets of leaders also do not trust each other."),
("Everest's Hillary Step: Has it gone or not?", "Days after a British mountaineer claimed that a famous rock feature near the summit of Mount Everest had disintegrated, two Nepali climbers have contradicted him.")]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(TableViewCell().classForCoder, forCellReuseIdentifier: cellIdentifier)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! TableViewCell
cell.showData(data: listData[indexPath.row])
return cell
}
}
TableViewCell
import UIKit
import SnapKit
class TableViewCell: UITableViewCell {
let titleLabel: UILabel = UILabel()
let contentLabel: UILabel = UILabel()
func showData(data:(String, String)) -> Void {
titleLabel.text = data.0
contentLabel.text = data.1
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.backgroundColor = UIColor.white
self.contentView.addSubview(titleLabel)
self.contentView.addSubview(contentLabel)
titleLabel.font = UIFont.systemFont(ofSize: 15)
titleLabel.textColor = UIColor.darkGray
titleLabel.numberOfLines = 0
titleLabel.lineBreakMode = .byWordWrapping
contentLabel.font = UIFont.systemFont(ofSize: 13)
contentLabel.textColor = UIColor.gray
contentLabel.numberOfLines = 0
contentLabel.lineBreakMode = .byWordWrapping
titleLabel.snp.makeConstraints { (make) in
make.left.right.equalToSuperview().inset(15)
make.top.equalToSuperview().inset(14)
}
contentLabel.snp.makeConstraints { (make) in
make.left.right.equalTo(titleLabel)
make.top.equalTo(titleLabel.snp.bottom).offset(5)
make.bottom.equalToSuperview().inset(15)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
效果圖: