如何在不重新加载整个表视图的情况下更新自定义节头?

时间:2017-04-24 01:08:45

标签: ios swift uitableview

我有一个自定义标头类来自定义我的tableview节标题。在部分标题的左侧是静态UILabel,而在右侧是计数UILabel,以计算该特定部分中的行数:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! CustomCell

    if section == 0
    {
        headerCell.reminder.text = "It Is Time"
        headerCell.count.text = String(arrayOne.count)
        headerCell.contentView.backgroundColor = UIColor(red: 230.0/255.0, green: 110.0/255.0, blue: 102.0/255.0, alpha: 1.0)
    }
    else if section == 1
    {
        headerCell.reminder.text = "It is not Time"
        headerCell.count.text = String(arrayTwo.count)
        headerCell.contentView.backgroundColor = UIColor(red: 113.0/255.0, green: 220.0/255.0, blue: 110.0/255.0, alpha: 1.0)
    }

    return headerCell.contentView
}

editActionsForRowAt中,我已实现了允许用户向左滑动以删除特定行的功能。但是,我希望获得对headerCell.count.text的访问权限并更新该部分标题中的行数,而无需调用tableView.reloadData来重新加载整个表格视图。

我在这里找到了类似的问题和答案:

  1. Changing UITableView's section header/footer title without reloading the whole table view
  2. Dynamically change the section header text on a static cell
  3. Update the header in tableView swift
  4. 然而,建议/解决方案并没有帮助我解决我的问题。

    如何在editActionsForRowAt中访问我的自定义标题标题单元格标签以更新计数?

    感谢。

2 个答案:

答案 0 :(得分:3)

尝试reloadSections方法:

在Swift中

let sectionToReload = 1
let indexSet: IndexSet = [sectionToReload]

self.tableView.reloadSections(indexSet, with: .automatic)

在目标c

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

答案 1 :(得分:0)

有一种hackish方式。不确定它是否会起作用,但值得尝试一下。 只需使用

tableView.beginUpdates()
tableView.endUpdates()

您不需要执行任何实际更新。我们的想法是,当tableView获得开始和结束更新的调用时,从理论上讲,它应该刷新对表的任何更改。我希望它会更新您案例中的节标题。如果有效,请告诉我。

相关问题