gpt4 book ai didi

ios - Swift4 tableview 单元格高度不是动态的并且始终是静态的

转载 作者:行者123 更新时间:2023-11-28 06:11:02 25 4
gpt4 key购买 nike

我真的需要帮助解决这个问题,我已经花了 6 天时间,但我找不到解决方案。

我想制作简单的表格 View ,但它的单元格始终具有静态高度,不会根据其中的项目改变

结果总是这样image from 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 个标签中,但结果如您之前所见

我尝试了所有方法,比如使用 Storyboard或代码制作 View ,但最后,我使用了名为 Stevia 的库,但结果仍然相同

这是 View Controller

    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()


}

}

这是我的 Storyboard :只包含表格 View 和单元格 inisde

my storyboard image

我真的需要帮助

最佳答案

终于找到问题了。

问题是我在创建 View 时忘记写底部最后一个 View 的底部约束

所以这部分

layout(
20,
|name1|,
20,
|name2|,
20,
|name3|,
20,
|name4|
)

应该换成这个

layout(
20,
|name1|,
20,
|name2|,
20,
|name3|,
20,
|name4|,
0
)

感谢所有试图帮助我的人...希望我的回答能帮到有sam问题的人

关于ios - Swift4 tableview 单元格高度不是动态的并且始终是静态的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46591464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com