Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't know why viewForHeaderInSection is not called in swift3

I don't know why viewForHeaderInSection is not called in swift3. I've added heightForHeaderInSection in UITableView but not called unfortunately. Please help me to check how to fixed it.

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
    }
}
like image 893
PPShein Avatar asked Nov 04 '16 02:11

PPShein


4 Answers

If someone else is having the same issue... Adding this line in viewDidLoad called the delegate methods therefore made the header appear:

self.tableView.estimatedSectionHeaderHeight = 80
like image 89
Giggs Avatar answered Nov 03 '22 02:11

Giggs


You sure you've added UITableViewDelegate, UITableViewDataSource in your ViewController Class

like image 31
Ken Nguyen Avatar answered Nov 03 '22 03:11

Ken Nguyen


In my case, adding self.tableView.sectionHeaderHeight = 80.0 to viewDidLoad did the magic.

like image 45
Sunkanmi Akintoye Avatar answered Nov 03 '22 02:11

Sunkanmi Akintoye


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. 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

  1. Automatic Height Calculation
  2. Automatic Estimated Height Calculation

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. Maybe it's a bug from xCode. Thanks

like image 1
Najam Avatar answered Nov 03 '22 03:11

Najam