【问题标题】:How to make footer UITableViewCell not static?如何使页脚 UITableViewCell 不是静态的?
【发布时间】:2018-05-24 09:11:34
【问题描述】:

我有 2 个原型动态单元格,分别称为 InvoiceDetailCell 和 TotalCostFooterCell。我使用viewForFooterInSection 将 TotalCostFooterCell 作为页脚单元格视图。这是我用来将数据分配给 UITableView 的代码

这是我在 UITableViewController 中的代码。

extension InvoiceDetailVC : UITableViewDataSource {
    // MARK: - UI Table View Datasource Methods

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return invoiceElements.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "InvoiceDetailCell", for: indexPath) as! InvoiceDetailCell
        cell.invoiceElementData = invoiceElements[indexPath.row]

        return cell
    }



}


extension InvoiceDetailVC : UITableViewDelegate {

    // MARK: - UI Table View Delegate Methods

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let cell = tableView.dequeueReusableCell(withIdentifier: "invoiceDetailFooterCell") as! TotalCostFooterCell
        cell.totalCost = singleInvoiceData.unpaid

        return cell
    }

}

但结果并不像我预期的那样,我的意思是页脚单元格是粘着/不动的。这是 .gif 文件:http://g.recordit.co/vf0iwCfEWX.gif

您可以看到总成本(红色)是粘性/静态的,我希望页脚单元格可以滚动并始终位于底部。还是我执行我想要的有错误?

【问题讨论】:

  • @alexa289 你是如何让单元格在页脚中将它们的总数相加的,我已经尝试了几天才能在页脚中添加总数跨度>

标签: ios swift uitableview


【解决方案1】:

将表格样式分组

你可以通过两种方式做到这一点:

  1. viewDidLoad()tableView.style = .grouped
  2. 从情节提要中选择表格视图,然后在属性检查器中将样式更改为分组。请参考附图。

【讨论】:

  • 很高兴看到它有帮助:D
【解决方案2】:

你不能把它作为表格视图的最后一行吗?我的意思是,该视图已经是一个表格视图单元格,因此将其用作最后一行是有意义的。

首先改变这个:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return invoiceElements.count + 1
}

然后对于cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == invoiceElements.count {
        let cell = tableView.dequeueReusableCell(withIdentifier: "invoiceDetailFooterCell") as! TotalCostFooterCell
        cell.totalCost = singleInvoiceData.unpaid

        return cell
    }
    let cell = tableView.dequeueReusableCell(withIdentifier: "InvoiceDetailCell", for: indexPath) as! InvoiceDetailCell
    cell.invoiceElementData = invoiceElements[indexPath.row]

    return cell
}

【讨论】:

    【解决方案3】:

    您可以通过以下方式设置您想要的自定义视图:

    tableView.tableFooterView = yourCustomView
    

    或者您可以像这样将所有内容放在您的 xib/storyboard 中:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 2018-07-09
      • 1970-01-01
      • 2016-12-03
      • 2012-04-24
      • 2013-01-13
      • 1970-01-01
      相关资源
      最近更新 更多