2

First off, let me say that I am using a UITableViewController with a custom header in my UITableView.

My custom header class is defined simply as so:

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

I load the custom header class like so:

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
}

Every time I delete a row in my table view from within editActionsForRowAt, I call self.tableView.reloadSections like so:

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)

}

Now calling self.tableView.reloadSections does work and updates the theCountLabel within my section header.

However, I've set UITableViewRowAnimation to none. But when I scroll further down the UITableView pass the current number of visible rows on screen and I delete a row, the section header disappears and reappears with the updated theCountLabel value.

I want to always keep my section header on top, i.e. not disappear and re-appear when the section is reloaded.

Is there any other way I can achieve this?

Thanks

Pangu
  • 3,721
  • 11
  • 53
  • 120

1 Answers1

5

Found a solution referenced by @Abhinav:

Reload tableview section without scroll or animation

Slight modification updated for Swift 3.0:

UIView.performWithoutAnimation {

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

}

Now if I scroll past the number of visible cells on screen and I delete a row, the headerCell.theCountLabel.text in my section header is updated without any animation, and remains still.

Community
  • 1
  • 1
Pangu
  • 3,721
  • 11
  • 53
  • 120