我有使用UITableViewCell筆尖製作的自定義標題視圖。每個自定義標題都有按鈕和圖片,需要根據用戶可以使用標題中的按鈕執行的不同操作進行更新。如何獲得/更新一個自定義UITableview標題視圖的引用?
我有難以得到參考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
}
好主意,但我無法將button.superview強制轉換爲自定義標題類。如果讓headerView = button.superview爲? Moment_StickyHeaderCell ... button.superview是UITableviewContentview –
比superview更多。 button.superview.superview會給你stickyheadercell。你也應該小心。您可以使遞歸方法,超級查看,直到右stickyheadercell類 – kekkeme