0

I'm setting header padding for my tableview in iOS older than 15 explicitly. This works for the top first header (section 1 of tableview), but doesn't work for second header (section 2 of tableview). How could I set it for the second also? Is there something like tableView.tableHeaderView[2] or tableView.tableHeaderView.second ? In code its the place with commented-out places.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if tableView.tag == 2 {
            if section == 0 {
                if self.tableView(tableView, numberOfRowsInSection: section) > 0 {
                    let view = UIView()
                    
                    let imageView = UIImageView(image: UIImage(named: "Logo_face"))
                    if #unavailable(iOS 15.0) {
                        imageView.frame = CGRect(x: 10, y: 20, width: 25, height: 25)
                    } else {
                        imageView.frame = CGRect(x: 10, y: 0, width: 25, height: 25)
                    }
                    
                    view.addSubview(imageView)
                    let label = UILabel()
                    label.text = "店舗"
                    label.frame = CGRect(x: 40, y: 0, width: 200, height: 25)
                    view.addSubview(label)
                    if #unavailable(iOS 15.0) { //setting here; works
                        let padding: CGFloat = 20
                        view.frame = CGRect(x: view.frame.origin.x - padding,
                                                  y: view.frame.origin.y - padding,
                                                  width: view.frame.width + 2 * padding,
                                                  height: view.frame.height + 2 * padding)

                        tableView.tableHeaderView = view // this is good
                        label.frame = CGRect(x: 40, y: 20, width: 200, height: 25)
                    }
                    return view
                }
            } else {
                if self.tableView(tableView, numberOfRowsInSection: section) > 0 {
                    let view = UIView()
                    let imageView = UIImageView(image: UIImage(named: "pinIconForSearch"))
                    if #unavailable(iOS 15.0) {
                        imageView.frame = CGRect(x: 10, y: 20, width: 25, height: 25)
                    } else {
                        imageView.frame = CGRect(x: 10, y: 0, width: 25, height: 25)
                    }
                    view.addSubview(imageView)
                    let label = UILabel()
                    label.text = "検索候補"
                    label.frame = CGRect(x: 40, y: 0, width: 200, height: 25)
                    view.addSubview(label)
                    if #unavailable(iOS 15.0) { //setting here doesnt work
                        let padding: CGFloat = 20
                        view.frame = CGRect(x: view.frame.origin.x - padding,
                                                  y: view.frame.origin.y - padding,
                                                  width: view.frame.width + 2 * padding,
                                                  height: view.frame.height + 2 * padding)

                        tableView.tableHeaderView = view // this is not good
                        label.frame = CGRect(x: 40, y: 20, width: 200, height: 25)
                    }
                    return view
                }
            }
        }
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0.0
        } else {}
        return nil
    }

Marcel Kopera
  • 91
  • 1
  • 7

0 Answers0