【发布时间】:2019-05-23 14:01:31
【问题描述】:
我有一个带有两个单元格的表格视图,表格视图单元格的内容会改变并且是动态的,所以我想动态调整表格视图单元格的大小并根据表格视图单元格高度计算表格视图的高度。 我的 tableview 在渲染单元格时占用了额外的空间。
谁能帮助我根据表格视图单元格高度设置表格视图高度的方法
class Recent_WH_acitivityCell: UITableViewCell {
@IBOutlet weak var wh_activity_tableView: UITableView!
@IBOutlet weak var wh_activity_table_ht: NSLayoutConstraint!
let cells = 2
var maxHeight: CGFloat = UIScreen.main.bounds.size.height
override func awakeFromNib() {
super.awakeFromNib()
wh_activity_tableView.estimatedRowHeight = 100
wh_activity_tableView.rowHeight = UITableViewAutomaticDimension
}
func setData()
{
wh_activity_tableView.reloadData()
self.layoutIfNeeded()
}
override var intrinsicContentSize: CGSize
{
let height = min(self.contentView.frame.height, maxHeight)
return CGSize(width: self.contentView.frame.width, height: height)
}
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsets(top: 0, left: 0, bottom: 15, right: 0))
}}
extension Recent_WH_acitivityCell : UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "recent_wh_data_cell", for: indexPath) as! Recent_WH_Data_cell
return cell
}
}
这是截图
请查看突出显示的部分。
【问题讨论】:
-
离题,快速的最佳实践不鼓励这样的命名
recent_wh_data_cell,更喜欢recentWhDataCell -
@Tobi 我想我现在主要关心的是我遇到的问题,而不是命名约定
-
一个小技巧,如题外注释^_^你可以随意使用它
-
是的,我会纠正它:)
-
您希望单元格 100% 适合表格视图的大小?
标签: ios swift uitableview