![](/img/trans.png)
[英]Why is my navigation bar disappearing on my ViewController and then re-appearing when I scroll down?
[英]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引用的解决方案:
对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.