[英]Swift4 tableview cell height not dynamic and always static
請真的我需要關於這個問題的幫助,我花了 6 天時間,但我找不到解決方案。
我想制作簡單的表格視圖,但它的單元格始終具有靜態高度,不會因其中的項目而改變
結果總是像emulator 的這個圖像。
我想在每個單元格中顯示此示例:
1 1 11 1 1111 1 1 11 11 11 1 1 1 11 1 1 11 11 1 11 1 11 11 11
22 22 2 2 2 2 2 2 2 2 2 2 2 22 22 22 22 2 2 22 2 2 2 2 22 2 2 22
33 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 33 33 3 3 3 3 3 333 3 3 3 3
444 44 4 44 444 4 4 4 4 4 4 4 4 4 4 44 4 4 44 4 4 4 4 4 4 4 44 44 4
它必須以垂直對齊方式顯示在 4 個標簽中,但結果如您之前所見
我嘗試了所有方法,例如使用故事板或代碼制作視圖,但最后,我使用了名為Stevia 的庫,但結果仍然相同
這是視圖控制器
class AttendanceControlPage:UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var dataTableView: UITableView!
@IBOutlet weak var navLeftButton: UIBarButtonItem!
//============================ Base Functions =================//
override func viewDidLoad() {
super.viewDidLoad()
// Some tutorials said to put this two lines but not changes
dataTableView.estimatedRowHeight = 44
dataTableView.rowHeight = UITableViewAutomaticDimension
initPage()
dataTableView.register(AttendanceControlCell.self, forCellReuseIdentifier: "cellCon")
}
//============================ Init Functions =================//
func initPage() {
startLoading()
self.navLeftButton.image = UIImage(named: "icn_drawer")
}
//==================== Nav Bar Buttons Actions ===================================//
@IBAction func reloadPage(_ sender: Any) {
self.dataArray.removeAll()
loadData();
}
@IBAction func navLeftButtonAction(_ sender: Any) {
showMenu()
}
//==================== Table view options ===================================//
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = dataTableView.dequeueReusableCell(withIdentifier: "cellCon", for: indexPath) as! AttendanceControlCell
return cell
}
}
這是細胞文件:
class AttendanceControlCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
initFunc(cell : self)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func initFunc(cell : UITableViewCell) {
let name1 = UILabel()
name1.numberOfLines = 0
name1.text = "1 1 11 1 1111 1 1 11 11 11 1 1 1 11 1 1 11 11 1 11 1 11 11 11 "
let name2 = UILabel()
name2.numberOfLines = 0
name2.text = "22 22 2 2 2 2 2 2 2 2 2 2 2 22 22 22 22 2 2 22 2 2 2 2 22 2 2 22 "
let name3 = UILabel()
name3.numberOfLines = 0
name3.text = "33 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 33 33 3 3 3 3 3 333 3 3 3 3 "
let name4 = UILabel()
name4.numberOfLines = 0
name4.text = "444 44 4 44 444 4 4 4 4 4 4 4 4 4 4 44 4 4 44 4 4 4 4 4 4 4 44 44 4 "
cell.contentView.sv(
name1,
name2,
name3,
name4
)
layout(
20,
|name1|,
20,
|name2|,
20,
|name3|,
20,
|name4|
)
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
這是我的故事板:僅包含帶有單元格 inisde 的 tableview
請我真的需要幫助
最后,我發現了問題。
問題是我在創建視圖時忘記寫底部最后一個視圖的底部約束
所以這部分
layout(
20,
|name1|,
20,
|name2|,
20,
|name3|,
20,
|name4|
)
應該用這個代替
layout(
20,
|name1|,
20,
|name2|,
20,
|name3|,
20,
|name4|,
0
)
感謝所有試圖幫助我的人......我希望我的回答能幫助任何有 sam 問題的人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.