我使用UITableViewCell笔尖制作自定义标题视图。每个自定义标题都有按钮和图片,需要根据用户可以使用标题中的按钮执行的不同操作进行更新。
我很难获得对headerView的引用来更新其UI。
我显然不能使用tableView.cellForRowAtIndexPath,因为它是一个标题而不是tableview单元格。我不能使用tableview.headerViewForSection,因为它是用UITableViewCell制作的,而不是UITableViewHeaderFooterView。
我也尝试过使用
tableView.reloadSections(indexSet, withRowAnimation: UITableViewRowAnimation.None)
但即使设置为UITableViewRowAnimation.None,这也会产生不必要的口吃/故障UI重新加载动画。毛刺似乎是标题消失,然后从顶部再次出现,页脚视图也从顶部向下滑动。
也尝试了
tableView.beginUpdates()
tableView.endUpdates()
这有着同样的瑕疵效应。
和
let header = tableView.headerViewForSection(button.tag)
//codes to update headerview here
header?.setNeedsDisplay()
header?.setNeedsLayout()
这没有效果。
任何帮助将不胜感激!
我的代码如下:
注册Nib:
let stickyHeaderNib = UINib(nibName: "Moment_StickyHeaderCell", bundle: nil)
tableView.registerNib(stickyHeaderNib, forCellReuseIdentifier: "moment_stickyHeaderCell")
然后:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let momentHeader = tableView.dequeueReusableCellWithIdentifier("moment_stickyHeaderCell") as! Moment_StickyHeaderCell
//various buttons and objects here
momentHeader.followButton.addTarget(self, action: "followButtonPressed:", forControlEvents: UIControlEvents.TouchDragInside)
return momentHeader}
然后选择按钮:
func followButtonPressed (button: UIButton) {
let moment = moments[button.tag]
let point = tableView.convertPoint(CGPointZero, fromView: button)
if let indexPath = tableView.indexPathForRowAtPoint(point) {
}
//here's where I can't get reference to the headerView as mentioned above using .cellForRowAtIndexPath, .headerViewForSection
}
答案 0 :(得分:1)
我发现这个帖子的回答[https://stackoverflow.com/a/3611661/4905076]女巫说的是:
self.tableView.beginUpdates()
self.tableView.endUpdates()
// forces the tableView to ask its delegate/datasource the following:
// numberOfSectionsInTableView:
// tableView:titleForHeaderInSection:
// tableView:titleForFooterInSection:
// tableView:viewForHeaderInSection:
// tableView:viewForFooterInSection:
// tableView:heightForHeaderInSection:
// tableView:heightForFooterInSection:
// tableView:numberOfRowsInSection:
因此,您应该在followButtonPressed
上手动更新您的部分,并调用开始/结束更新。
答案 1 :(得分:-1)