簡體   English   中英

如何僅引用/更新一個自定義UITableview標頭視圖?

[英]How to get reference to/update just one custom UITableview header view?

我有使用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

   }

我發現這個帖子的答案[ 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手動更新您的部分,並調用開始/結束更新。

按鈕是標題視圖的子視圖。 您可以通過button.superview獲得

查看圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM