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

标签 ios swift uitableview

首先,我要说的是,我在 UITableView 中使用了带有自定义 header 的 UITableViewController

我的自定义 header 类的定义如下:

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

我像这样加载自定义 header 类:

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 中删除表格 View 中的一行时,我都会调用 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引用的解决方案:

Reload tableview section without scroll or animation

针对 Swift 3.0 进行了轻微修改:

UIView.performWithoutAnimation {

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

}

现在,如果我滚动超过屏幕上可见单元格的数量并删除一行,则节标题中的 headerCell.theCountLabel.text 会更新,没有任何动画,并保持静止。

关于ios - 如何在 Swift 中重新加载节标题而不使其消失/重新出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674619/

相关文章:

ios - Xcode 卡在 'waiting for other symbol processing'

ios - 无法打开文件 “Main.strings”,因为没有这样的文件。

iphone - 没有替换图像和单元格的 UITableView 重绘

ios - map 中心和屏幕中心不同

const 属性的 Swift inout 参数

swift - 更改 UITableViewCell 的 intrinsicContentSize 是如何工作的?

iphone - 当单元格上出现删除按钮时隐藏部分索引标题

ios - 释放内存 Dismissing Modal View Controller

ios - 如何调整 UICollectionView 的高度为 UICollectionView 内容大小的高度?

ios - 如何在 UITable 中获取 UIButton 或 UILabel 的位置?