2016-11-04 86 views
1

我不知道爲什麼viewForHeaderInSection沒有在swift3中調用。我在UITableView中添加了heightForHeaderInSection,但不幸的是沒有調用。請幫我檢查一下如何修復它。我不知道爲什麼viewForHeaderInSection沒有在swift3中調用

enter image description here

func numberOfSectionsInTableView(_ tableView: UITableView) -> Int { 
    return 3 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    switch (section) { 
    case 0: 
     return homeStrs.count 
    case 1: 
     return accountStrs.count 
    case 2: 
     return otherStrs.count 
    default: 
     return otherStrs.count 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { 
    let cell = menuTable.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath) as! MenuCell 
    cell.layoutMargins = UIEdgeInsets.zero; 
    cell.lbNoti.isHidden = true 
    switch ((indexPath as NSIndexPath).section) { 
    case 0: 
     cell.lbMenuTitle?.text = homeStrs[(indexPath as NSIndexPath).row] 
     cell.imgIcon.image = UIImage (named: homeImgStrs[(indexPath as NSIndexPath).row]) 
    case 1: 
     cell.lbMenuTitle?.text = accountStrs[(indexPath as NSIndexPath).row] 
     cell.imgIcon.image = UIImage (named: accountImgStrs[(indexPath as NSIndexPath).row]) 
    case 2: 
     cell.lbMenuTitle?.text = otherStrs[(indexPath as NSIndexPath).row] 
     cell.imgIcon.image = UIImage (named: otherImgStrs[(indexPath as NSIndexPath).row]) 
    default: 
     cell.lbMenuTitle?.text = "Other" 
    } 

    return cell 
} 

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 40 
} 

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    switch (section) { 
    case 1: 
     let headerView = UIView(frame: CGRect(x: 0, y: 0, width: self.menuTable.frame.size.width, height: 30)) 
     let menuHeaderLabel = UILabel(frame: CGRect(x: 20, y: 0, width: self.menuTable.frame.size.width, height: 28)) 
     menuHeaderLabel.text = "Account Settings" 
     headerView.addSubview(menuHeaderLabel) 
     return headerView 
    case 2: 
     let headerView = UIView(frame: CGRect(x: 0, y: 0, width: self.menuTable.frame.size.width, height: 30)) 
     let menuHeaderLabel = UILabel(frame: CGRect(x: 20, y: 0, width: self.menuTable.frame.size.width, height: 28)) 
     menuHeaderLabel.text = "Others" 
     headerView.addSubview(menuHeaderLabel) 
     return headerView 
    default: 
     let headerView = UIView(frame: CGRect(x: 0, y: 0, width: self.menuTable.frame.size.width, height: 0)) 
     return headerView 
    } 
} 

回答

3

您確認您已加入的UITableViewDelegate,UITableViewDataSource在你的ViewController類

+1

這是自定義tableview,我附加了故事板中的委託和數據源方法。順便說一句,我已經編輯我的答案,顯示附加的委託和數據源在故事板。 – ppshein

+1

「這是自定義tableview,我在storyboard中附加了委託和數據源方法」這不夠好。您的視圖控制器必須_explicitly採用_ UITableViewDelegate和UITableViewDataSource或您的方法將不會被調用。 – matt

+0

@matt很棒。謝謝你的幫助。 – ppshein

12

如果別人有同樣的問題...添加在viewDidLoad中這條線叫委託方法,因此使標題出現:

self.tableView.estimatedSectionHeaderHeight = 80 
1

在我的情況下,將self.tableView.sectionHeaderHeight = 80.0添加到viewDidLoad d發現魔法。

0

同樣的問題發生與我,是因爲的tableview發現在計算頭高度困難,但我是用高度自動計算的Xcode 9,如上面提到的,我不能給任何明確的高度值。一些實驗後,我得到了解決,我們必須重寫此方法,

-(CGFloat)tableView:(UITableView *)tableView 
     estimatedHeightForHeaderInSection:(NSInteger)section 
{ 
     return 44.0f; 
} 

雖然我檢查兩個選項

  1. 高度自動計算
  2. 自動預測的高度計算

故事板如蘋果說,但我仍然得到這個奇怪的錯誤。

請注意:此錯誤是隻能顯示在IOS-10版本不IOS-11版本。也許這是來自xCode的錯誤。謝謝

相關問題