2016-11-14 79 views
1

我們有一個TableViewModel這是實施UITableViewDelegateUITableViewDataSource。它包含部分並返回我們的UITableViews的正確行和元素。UITableView自定義頁腳

現在我們想要一個自定義表格視圖部分頁腳。因此,我們實施optional public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?,以便從xib文件返回UIView。這一切都很好,但最終會出現錯誤的頁腳高度。如果我們使用func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?頁腳顯示正確,頁腳高度設置正確。

所以我們也必須實現optional public func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloatoptional public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat

  1. 問:我如何獲得此實例化一個UIView的高度? view.frame.boundsview.frame.height始終爲0.
  2. 問題:Apple對頁腳使用的默認值是多少?它是如何計算的?我認識到,如果我們返回table.sectionFooterHeight作爲默認值(不是我們的自定義頁腳視圖),高度是正確的。

TableViewModel:

class TableViewModel: NSObject, UITableViewDelegate, UITableViewDataSource { 

    var sections: [TableViewSection] = [] 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return sections.count 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return sections[section].numberOfRowsInTableView(tableView) 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     return sections[(indexPath as NSIndexPath).section].tableView(tableView, cellForRowAtIndexPath: indexPath) 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     sections[(indexPath as NSIndexPath).section].tableView(tableView, didSelectRowAtIndexPath: indexPath) 
    } 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return sections[section].headerText 
    } 

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
     sections[section].tableView(tableView, willDisplayHeaderView: view) 
    } 

    func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { 
     return sections[section].footerText 
    } 

    func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { 
     sections[section].tableView(tableView, willDisplayFooterView: view) 
    } 

    func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool { 
     return sections[(indexPath as NSIndexPath).section].tableView(tableView, cellContentsToCopyAtIndexPath: indexPath, withSender: nil) != nil 
    } 

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
     ?? 
    } 

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
     return sections[section].footerView 
    } 

    func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat { 
     ?? 
    } 
} 
+0

您是否嘗試在設置titleForFooterInSection時將變量的高度保存在變量中?例如:footerHeight =節[section] .footerText.frame.bounds.height,其中footerHeight是控制器中的變量,您可以將其用作'heightForFooterInSection'的返回值 –

+0

您的頁腳是否有固定的高度?或者是他們的高度動態並由AutoLayout設置? – paulvs

+0

是的,但這意味着我必須在創建頁腳時設置高度。我無法知道它在每個設備上的高度(包含字符串,因此動態高度取決於手機的大小)。不知何故,蘋果的界面構建者確實在默認視圖上設置了高度。 – BennX

回答

0

我加入下面的方法解決它:

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    return sections[section].footerViewHeight ?? UITableViewAutomaticDimension 
} 

並返回在部分頁腳計算高度。它確實調整大小,我們可以調整它。如上所述,返回UITableViewAutomaticDimension需要:

// Returning this value from tableView:heightForHeaderInSection: or tableView:heightForFooterInSection: results in a height that fits the value returned from // tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: if the title is not nil. 
-1

如果您正在使用自動佈局你仍然可以在heightForFooterInSection返回UITableViewAutomaticDimension和頁腳的高度將自動計算。與heightForRowAt相同,heightForFooterInSection必須與estimatedHeightForFooterInSection結合使用。一個簡單的例子是這樣的:

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat { 
    return 18 // or whatever value 
} 
+0

感謝您的回答。我會試試看看它是如何工作的。無線提供反饋。 – BennX

+0

//從tableView:heightForHeaderInSection:或tableView:heightForFooterInSection:或tableView:heightForFooterInSection:返回值的高度適合從 返回的值// tableView:titleForHeaderInSection:或tableView:titleForFooterInSection:如果標題不是零.' – BennX

+0

@BennX你從哪裏得到這個?在我的其中一個項目中,按預期工作 –