2016-02-14 64 views
0

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

    } 

回答

-1

按鈕是頭部的子視圖視圖。您可以通過button.superview

Check Image

+0

好主意,但我無法將button.superview強制轉換爲自定義標題類。如果讓headerView = button.superview爲? Moment_StickyHeaderCell ... button.superview是UITableviewContentview –

+0

比superview更多。 button.superview.superview會給你stickyheadercell。你也應該小心。您可以使遞歸方法,超級查看,直到右stickyheadercell類 – kekkeme

0

我發現這個帖子答案[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更新部分,並呼籲開始/結束更新。

+0

我試過這個,我得到像reloadSections方法中的毛病/結構化UI更新動畫,不幸的是 –

+0

你試圖改變動畫爲.default或其他? – Lucho

+0

是的...我嘗試使用動畫。無 –

相關問題