首先,我要说的是,我在 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/