我不知道为什么在swift3中没有调用viewForHeaderInSection

PPS*_*ein 4 uitableview ios swift swift3

我不知道为什么viewForHeaderInSection不在swift3中调用.我添加heightForHeaderInSection了UITableView但不幸的是没有调用.请帮我检查一下如何修理它.

在此输入图像描述

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
    }
}
Run Code Online (Sandbox Code Playgroud)

Gig*_*ggs 14

如果其他人遇到同样的问题...在viewDidLoad中添加此行称为委托方法因此使标题出现:

self.tableView.estimatedSectionHeaderHeight = 80
Run Code Online (Sandbox Code Playgroud)


小智 6

您确定已在ViewController类中添加了UITableViewDelegate,UITableViewDataSource

  • “这是自定义表格视图,我在情节提要中附加了委托和数据源方法”,这还不够好。您的视图控制器必须_explicitly采用_ UITableViewDelegate和UITableViewDataSource,否则您的方法将不会被调用。 (2认同)