【问题标题】:How to get reference to/update just one custom UITableview header view?如何仅获取一个自定义 UITableview 标题视图的引用/更新?
【发布时间】: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


    【解决方案1】:

    我找到这个帖子的答案 [https://stackoverflow.com/a/3611661/4905076]witch is says that call:

    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 上手动更新您的部分并调用开始/结束更新。

    【讨论】:

    • 我已经尝试过了,不幸的是,就像在 reloadSections 方法中一样,我得到了故障/断断续续的 UI 更新动画
    • 您是否尝试将动画更改为 .default 或其他内容?
    【解决方案2】:

    按钮是标题视图的子视图。可以通过 button.superview 获取

    【讨论】:

    • 好主意,但我无法将 button.superview 转换为自定义标题类。如果让 headerView = button.superview 为? Moment_StickyHeaderCell...button.superview 是 UITableviewContentview
    • 多了一个超级视图。 button.superview.superview 会给你stickyheadercell。你也应该小心。您可以制作递归方法,直到正确的stickyheadercell类
    猜你喜欢
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2012-08-18
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多