[英]I don't know why viewForHeaderInSection is not called in swift3
I don't know why viewForHeaderInSection
is not called in swift3. 我不知道为什么在
viewForHeaderInSection
中没有调用viewForHeaderInSection。 I've added heightForHeaderInSection
in UITableView but not called unfortunately. 我在UITableView中添加了
heightForHeaderInSection
,但遗憾的是没有调用。 Please help me to check how to fixed it. 请帮我检查一下如何修理它。
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
}
}
如果其他人遇到同样的问题...在viewDidLoad中添加此行称为委托方法因此使标题出现:
self.tableView.estimatedSectionHeaderHeight = 80
您确定在ViewController类中添加了UITableViewDelegate,UITableViewDataSource
在我的例子中,将self.tableView.sectionHeaderHeight = 80.0
添加到viewDidLoad就可以了。
Same issue occured with me that's because tableview find difficulties in calculating height for header but as I was using automatic height calculation from xCode 9 , I cannot give any explicit height value as mentioned above. 同样的问题发生在我身上,因为tableview在计算标题高度方面遇到困难,但是当我使用xCode 9的 自动高度计算时 ,我不能给出任何明确的高度值,如上所述。 After some experimentation I got solution , we have to override this method as,
经过一些实验,我得到了解决方案 ,我们必须覆盖这个方法,因为,
-(CGFloat)tableView:(UITableView *)tableView
estimatedHeightForHeaderInSection:(NSInteger)section
{
return 44.0f;
}
Although I have checked both options 虽然我已经检查了两个选项
from storyboard as apple says, but still I got this weird error. 苹果说,从故事板 ,但我仍然有这个奇怪的错误。
Please Note : This Error was shown only on IOS-10 version not on IOS-11 version. 请注意 :此错误仅在IOS-10版本上显示,而不在IOS-11版本上显示。 Maybe it's a bug from xCode.
也许这是xCode的一个bug。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.