繁体   English   中英

如何在Swift中重新加载节标题而又不消失/不出现?

[英]How to reload a section header without it disappearing/re-appearing in Swift?

首先,让我说我正在UITableViewController使用带有自定义标头的UITableView

我的自定义标头类的定义很简单:

class HeaderCell: UITableViewCell
{
    @IBOutlet var theLabel: UILabel!
    @IBOutlet var theCountLabel: UILabel!

    override func awakeFromNib()
    {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

我像这样加载自定义标头类:

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

    if section == 0
    {
        headerCell.theLabel.text = "Test 1"
        headerCell.theCountLabel.text = String(myArrayOne.count)
    }
    else if (section == 1)
    {
        headerCell.theLabel.text = "Test 2"
        headerCell.theCountLabel.text = String(myArrayTwo.count)
    }

    return headerCell.contentView
}

每次我从editActionsForRowAt内的表视图中删除一行时,都会这样调用self.tableView.reloadSections

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
{
    ...

    var indexSet: IndexSet = IndexSet()

    indexSet.insert(indexPath.section)

    if indexPath.section == 0
    {
        self.myArrayOne.remove(at: indexPath.row)
    }
    else if indexPath.section == 1
    {
        self.myArrayTwo.remove(at: indexPath.row)
    }

    self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.none)
    self.tableView.reloadSections(indexSet, with: UITableViewRowAnimation.none)

}

现在调用self.tableView.reloadSections可以正常工作并更新我的节标题中的theCountLabel

但是,我已经将UITableViewRowAnimation设置为none 但是,当我进一步向下滚动UITableView传递当前在屏幕上可见的行数并删除一行时,节标题消失,并以更新的theCountLabel值重新出现。

我想始终将我的节标题保持在顶部,即在重新加载节时不会消失并不会再次出现。

我还有其他方法可以实现这一目标吗?

谢谢

找到了@Abhinav引用的解决方案:

重新加载tableview部分,无需滚动或动画

Swift 3.0进行了轻微修改:

UIView.performWithoutAnimation {

    self.tableView.beginUpdates()
    self.tableView.reloadSections(indexSet, with: UITableViewRowAnimation.none)
            self.tableView.endUpdates()

}

现在,如果我滚动查看屏幕上可见单元格的数量并删除一行,则我的节标题中的headerCell.theCountLabel.text将更新而不会产生任何动画,并且保持不变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM