【问题标题】:How to update a custom section header without reloading the whole table view?如何在不重新加载整个表格视图的情况下更新自定义部分标题?
【发布时间】:2017-09-20 13:20:36
【问题描述】:

我有一个自定义标题类来自定义我的 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

但是,这些建议/解决方案并没有帮助我解决我的问题。

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

谢谢。

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    试试reloadSections方法:

    在斯威夫特中

    let sectionToReload = 1
    let indexSet: IndexSet = [sectionToReload]
    
    self.tableView.reloadSections(indexSet, with: .automatic)
    

    在目标c中

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

    【讨论】:

    • self.tableView.reloadSections 正是我使用的,它在editActionsForRowAt 中正确更新了我的部分标题标签
    【解决方案2】:

    有一种骇人听闻的方式。不确定它是否会起作用,但值得一试。 只需使用

    tableView.beginUpdates()
    tableView.endUpdates()
    

    您不需要执行任何实际更新。这个想法是,当 tableView 收到开始和结束更新的调用时,从理论上讲,它应该刷新对表所做的任何更改。我希望它会在您的情况下更新节标题。让我知道它是否有效。

    【讨论】:

    • 这在放入editActionsForRowAt时不起作用...计数标签不更新
    猜你喜欢
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    相关资源
    最近更新 更多