【发布时间】:2017-02-03 07:46:59
【问题描述】:
我创建了 UIViewController 并在其上添加了 UITableView(通过自动布局固定到所有四个边缘)。
然后我设置 estimatedRowHeight (44) 和 rowHeight (UITableViewAutomaticDimension) 并返回 5 个自定义单元格。它奏效了。
现在,我想添加具有动态高度的自定义 UITableViewHeaderFooterView。
接下来我会做:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 88.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return R.nib.orderStatusHeaderView.firstView(owner: self)!
}
我的 OrderStatusHeaderView 是一个 xib 视图,其 UITableView 使用自动布局固定到所有 4 个边缘。
OrderStatusHeaderView:
final class OrderStatusHeaderView: UITableViewHeaderFooterView {
@IBOutlet weak var tableView: UITableView! {
didSet {
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = 44.0
}
}
}
extension OrderStatusHeaderView: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "\(indexPath.row)")
cell.textLabel?.text = "\(indexPath.row)"
cell.backgroundColor = .red
return cell
}
}
这显示如下:
当我点击或滚动时,所有红色单元格都会消失。会是什么呢?以及如何使UITableView 动态加载内容和UITableViewHeaderFooterView 将自行调整大小以适应UITableView.contentSize。有可能吗?
【问题讨论】:
标签: ios uitableview