簡體   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