首页 > 解决方案 > 静态表格视图如何使最后一个可见行占据剩余空间

问题描述

有 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 作为高度来隐藏它。

那么如何让最后一个可见单元格展开并占据全部空间

标签: iosswiftuitableviewuitableviewautomaticdimension

解决方案


添加一个空白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
}

然后,无论何时更改行(基于用户交互或其他),调用reloadDatatableView,然后调用updateTable()以重新计算最后一个可见行及其高度。


推荐阅读