【问题标题】:Static tableview How do I make the last visible row to occupy remaining space in静态表格视图如何使最后一个可见行占据剩余空间
【发布时间】:2021-11-10 03:26:33
【问题描述】:

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

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

【问题讨论】:

  • 我想你正在寻找这个 infoTable.tableFooterView = UIView() 在 vi​​ewDidLoad 中写这个来删除底部多余的行。或者使用你的方法indexPath.row == arrayData.count - 1比较最后一个索引。
  • 嗨,我希望最后一个可见的空间占据其余空间。使其出现在屏幕底部
  • 我想显示最后一个可见行以占用剩余空间。假设第三个索引是最后可见的,第四个和第五个高度为零,因此它不可见。现在我如何确保第三行占据 tableview 的剩余高度

标签: ios swift uitableview uitableviewautomaticdimension


【解决方案1】:

添加一个空的UIView 作为表尾视图:

class StaticTableViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
    }
    
}

编辑 重新阅读您的问题后...

以上代码将“隐藏”空行。

如果您的目标是让“最后一个可见行”填充剩余的高度,那将更加复杂。

由于您当前的方法是使用静态表格视图并且您不会进行任何滚动,因此更简单的方法是使用带有自定义视图而不是单元格的 UIStackView。然后一切都将由自动布局、约束和堆栈视图处理,您无需进行任何计算。


Edit 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
}

然后,只要您更改行(基于用户交互或其他),请在 tableView 上调用 reloadData,然后调用 updateTable() 以重新计算最后一个可见行及其高度。

【讨论】:

  • 是的,你是对的,但由于遗留代码,我将不得不坚持使用 Tableview。我的目标是让“最后一个可见行”填充剩余的高度。任何建议表示赞赏
  • @RaviMan - 好吧,可能有很多“移动部件”需要处理。您何时确定要显示哪些行?在viewDidLoad()?或者,您是否根据用户交互动态显示/隐藏行?您的静态单元格的高度是固定的,还是高度会根据视图宽度而变化?我建议编辑您的问题并包括“真实世界”单元格内容(我假设您发布的图像并不是您计划显示的内容),以及有关如何以及何时确定哪些行的更多详细信息你想显示和隐藏。
  • @RaviMan - 好吧,不......你在heightForRowAt中设置行高......大概,你在某处设置了一些数据之前 i> 调用,hideRow(at: indexPath) 根据该数据返回 true 或 false。那么 - 您在哪里/何时设置该数据?
  • @RaviMan - 您可以根据需要使用该代码。请参阅Edit 2a了解代码安排和注释。
猜你喜欢
  • 2019-04-22
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 2010-11-06
  • 1970-01-01
  • 2019-01-19
相关资源
最近更新 更多