【发布时间】:2016-02-14 05:42:03
【问题描述】:
我有使用 UITableViewCell nib 制作的自定义标题视图。每个自定义标题都有按钮和图片,需要根据用户可以使用标题中的按钮执行的不同操作进行更新。
我很难引用 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()
这没有任何影响。
任何帮助将不胜感激!
我的代码如下所示:
注册笔尖:
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
}
【问题讨论】:
标签: ios swift uitableview