ios - 静态表格视图如何使最后一个可见行占据剩余空间
问题描述
有 5 个静态单元格。当我显示所有 5 个单元格时,这很好,但是如果我通过将高度返回为零来隐藏某个单元格,并且最后一个可见单元格与情节提要中 tableview 的最后一个单元格不同,那么关于如何做到这一点会产生混淆?
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return hideRow(at: indexPath) ? 0: UITableView.automaticDimension
}
IMO,如果 tableview 中的最后一个可见行和 tableview 中的 lastrow 相同
return indexPath.row == 4 ? 150 : tblHeight - 150
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if hideRow(at: indexPath) {
return 0
}
else {
let tblHeight = infoTable.bounds.size.height
if indexPath.row == 4 {
return tblHeight - yPlusHeightOfPreviousCell
}
return UITableView.automaticDimension
}
}
但是现在的问题是,如果第 3 行是最后一行,而对于第 4 行和第 5 行,我将返回 0 作为高度来隐藏它。
那么如何让最后一个可见单元格展开并占据全部空间
解决方案
添加一个空白UIView
作为表格页脚视图:
class StaticTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
}
编辑 重新阅读您的问题后...
上面的代码将“隐藏”空行。
如果您的目标是让“最后一个可见行”填充剩余的高度,那将更加复杂。
由于您当前的方法是使用静态表格视图并且您不会进行任何滚动,因此更简单的方法是使用UIStackView
自定义视图而不是单元格。然后一切都将由自动布局、约束和堆栈视图处理,您无需进行任何计算。
编辑 2a -将代码移动到函数
将这些变量添加到您的表视图控制器类中:
var numRows: Int = 5
var lastVisibleRow: Int = -1
var lastRowHeight: CGFloat = -1
var curTableWidth: CGFloat = 0
添加此功能:
func updateTable() -> Void {
lastVisibleRow = -1
var h: CGFloat = 0
// find last visible row
// and sum of row heights
for i in 0..<numRows {
let r = tableView.rectForRow(at: IndexPath(row: i, section: 0))
h += r.height
if r.height > 1.0 {
lastVisibleRow = i
}
}
// get rect of last visible row
let r = tableView.rectForRow(at: IndexPath(row: lastVisibleRow, section: 0))
// subtract its height from the sum of heights
h -= r.height
// set calculated last row height
lastRowHeight = tableView.frame.height - h + tableView.bounds.origin.y
// reload the last visible row
//tableView.reloadRows(at: [IndexPath(row: lastVisibleRow, section: 0)], with: .none)
tableView.reloadData()
}
viewDidLayoutSubviews()
在表格宽度发生变化的任何时候调用它:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// we only run the following block
// if the tableView width has changed
if curTableWidth != tableView.frame.width {
curTableWidth = tableView.frame.width
updateTable()
}
}
并将您的更改heightForRowAt
为:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if hideRow(at: indexPath) {
// row should be hidden
return 0
} else if indexPath.row == lastVisibleRow {
// if it's the last visible row,
return lastRowHeight
}
// else
return UITableView.automaticDimension
}
然后,无论何时更改行(基于用户交互或其他),调用reloadData
tableView,然后调用updateTable()
以重新计算最后一个可见行及其高度。
推荐阅读
- calculated-columns - 如何根据其 3 个相邻行的最小值和最大值插入新列?
- python - rioxarray(或xarray)在使用to_netcdf重新投影和保存后将spatial_ref坐标转换为变量?
- javascript - 为什么我收到“TypeError:this.state.users.map is not a function”,用于部署的 React 应用程序的生产版本
- javascript - MuseJSAssert:调用选择器函数时出错:TypeError:this.elem.get(...) 未定义
- linux - zsh 打印转义字符以记录
- unity3d - *已解决*我无法让我的精灵在 Unity 中触发对撞机
- acumatica - 将自定义字段中的值复制到 RowPersisting 事件中的 TaxRegistrationID 字段
- laravel - 使用标头参数的邮递员授权工作但不适用于 Axios 标头
- apache - 我正在尝试使用 ssl xampp 创建自签名证书,但出现以下错误
- ms-access - 带有查询参数的函数中的 OpenRecordSet