2015-04-20 47 views
1

您好我想知道如何去爲我的UITableView實現自定義標題,並使用它的自動佈局進行正確定位。在UITable上設置自定義HeaderView,使用Nib的AutoLayout

我現在可以顯示單元格,但是沒有應用水平或垂直自動佈局。

在我tableViewController

,我一個headerView變量設置爲我的自定義筆尖這樣:

@IBOutlet var view: UIView! 

override init(frame: CGRect) { // for using CustomView in code 
    super.init(frame: frame) 
    self.setup() 
} 

required init(coder aDecoder: NSCoder) { // for using CustomView in IB 
    super.init(coder: aDecoder) 
    self.setup() 
} 

private func setup() { 
    NSBundle.mainBundle().loadNibNamed("CustomHeader", owner: self, options: nil) 
    self.addSubview(view) 
} 

這在視圖類來電:

override init(frame: CGRect) { // for using CustomView in code 
    super.init(frame: frame) 
    self.setup() 
} 

required init(coder aDecoder: NSCoder) { // for using CustomView in IB 
    super.init(coder: aDecoder) 
    self.setup() 
} 

private func setup() { 
    NSBundle.mainBundle().loadNibNamed("ChallengeHeader", owner: self, options: nil) 
    self.addSubview(view) 
} 

它有一些假的內容和我設置動態文本到裏面的標籤。約束條件設置正確,我對錶格單元格工作正常。

我有這樣的顯示視圖標題

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    if let view = headerView { 
     return view 
    } 
    return nil 
} 


override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    if let head = headerView { 
     head.setNeedsLayout() 
     head.layoutIfNeeded() 
     let height = head.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height 

     let headerFrame = head.frame 


     Debug.log("Header Height \(height)") 
     return 100.0 
    } 
    return 0 
} 

目前我回國,因爲高度100.0始終爲0,所以我是什麼在這裏失蹤,使自動版式做的工作?或者我需要以編程方式設置f/e寬度?

編輯:忘了補充一下,xib的寬度爲400,好像也保持了400的寬度。

回答

6

您的實施有幾個問題。

首先,您不應該添加headerView作爲self.view的子視圖。其次,如果您有多個部分,則每個部分都需要一個新的CustomView,您不應該重複使用它。

所以,你的代碼應該是這個樣子:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    return NSBundle.mainBundle().loadNibNamed("CustomHeader", owner: nil, options: nil)[0] as? UIView 
} 

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height 
} 

你不需要任何幀操作。只要確保你在viewForHeaderInSection中返回了正確初始化的CustomHeader,並且一切都應該正常工作。

如果您需要更多幫助或支持,請讓我知道。


這是一個小的代碼片段,我用來測試 - 您的情況,唯一的區別是,你從廈門國際銀行加載headerView:

override func viewDidLoad(){ 

    headerView = UIView.new() // here you should have your xib loading 

    var insideView = UIView.new(); 
    insideView.setTranslatesAutoresizingMaskIntoConstraints(false) 
    headerView.addSubview(insideView); 

    // This is simulating the constraints you have in your xib 
    headerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[view(50)]-|", options: nil, metrics: nil, views: ["view": insideView]))  
    headerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[view]-|", options: nil, metrics: nil, views: ["view": insideView])) 
} 

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    return headerView 
} 

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height 
} 
+0

我需要像幀操作,因爲它是一個自動佈局文件。 我也沒有超過1個部分,所以重新使用它似乎是一個好主意給我。我會研究其他建議,這可能會有所幫助。 視圖高度無限,因爲它包含一些用戶生成的內容。 –

+0

我已經更新了我的答案。如果您的約束在xib文件中正確設置,則這應該起作用。重要的是,headerView的高度必須通過它的子視圖來確定,使用正確的約束。如果它不起作用,你可以發佈一些關於約束的更多信息,我很樂意幫助 –

+0

這個:首先,你不應該添加headerView作爲self.view的子視圖。 那麼適當的解決方案是什麼?我還沒有看到任何其他方式待辦事,可能是因爲我的頭太忙,真的與牆壁親密。 –

相關問題