2016-11-04 93 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的错误。谢谢

相关问题