-1

I did collapse/expand table cell in swift 3. But, I would like to add some padding bottom to the title header. In my screenshot, there is no padding between Title 1 and Title 2.

Another problem is that how can I move arrow image to the right?

enter image description here

Here is my code.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: 300, height: 28))
    var imageView = UIImageView()
    if (expandSections.contains(section)) {
        imageView = UIImageView.init(frame: CGRect(x: 7, y: 5, width: 25, height: 25))
        imageView.image = UIImage(named: "down_image")

    } else {
        imageView = UIImageView.init(frame: CGRect(x: 7, y: 5, width: 25, height: 25))
        imageView.image = UIImage(named: "up_image")
    }

    let headerTitle = UILabel.init(frame: CGRect(x: 38, y: 4, width: 250, height: 28))
    headerTitle.text = sectionData[section]
    headerTitle.textColor = UIColor.white

    let tappedSection = UIButton.init(frame: CGRect(x: 0, y: 0, width: headerView.frame.size.width, height: headerView.frame.size.height))
    tappedSection.addTarget(self, action: #selector(sectionTapped), for: .touchUpInside)
    tappedSection.tag = section

    headerView.addSubview(imageView)
    headerView.addSubview(headerTitle)
    headerView.addSubview(tappedSection)
    headerView.backgroundColor = UIColor.lightGray
    return headerView
}
May Phyu
  • 895
  • 3
  • 23
  • 47

1 Answers1

0

Try this, In here I added another view inside of headerView. Then only u can add padding in headerView.

  func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: 300, height: 28))

        let internalView = UIView.init(frame: CGRect(x: 0, y: 0, width: 300, height: 25))

        var imageView = UIImageView()

        if (expandSections.contains(section)) {

            imageView = UIImageView.init(frame: CGRect(x: internalView.frame.size.width - 10 , y: 5, width: 25, height: 25))
            imageView.image = UIImage(named: "down_image")

        } else {
            imageView = UIImageView.init(frame: CGRect(x: internalView.frame.size.width - 10 , y: 5, width: 25, height: 25))
            imageView.image = UIImage(named: "up_image")
        }

        let headerTitle = UILabel.init(frame: CGRect(x: 38, y: 4, width: 250, height: 28))
        headerTitle.text = sectionData[section]
        headerTitle.textColor = UIColor.white

        let tappedSection = UIButton.init(frame: CGRect(x: 0, y: 0, width: headerView.frame.size.width, height: headerView.frame.size.height))
        tappedSection.addTarget(self, action: #selector(sectionTapped), for: .touchUpInside)
        tappedSection.tag = section

        internalView.addSubview(imageView)
        internalView.addSubview(headerTitle)
        internalView.addSubview(tappedSection)

        headerView.addSubview(internalView)
        headerView.backgroundColor = UIColor.lightGray
        return headerView
    }
GayashanK
  • 1,195
  • 2
  • 13
  • 27
  • Bro @Sasquatch, actually I would like to be totally separate these two titles and see the background color table. How can I do this? – May Phyu Apr 04 '17 at 10:19
  • cell. headerView.backgroundColor = UIColor.tablebackgroundColor , then it works – GayashanK Apr 04 '17 at 10:22
  • When I declare like this for cell. let cell = tableView.dequeueReusableCell(withIdentifier: "buy", for: indexPath) as! PlanTableCell. I got error. – May Phyu Apr 04 '17 at 10:27
  • did you make customCell. If you make customSell you need to register it – GayashanK Apr 04 '17 at 10:30
  • Bro @Sasquatch The error is--> indexPath is unresolved identifier 'indexPath'. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {} . In this code, viewForHeaderInSection section: Int. When I change Int to indexPath. I got error too. – May Phyu Apr 04 '17 at 10:30
  • let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "buy") as! PlanTableCell! , add this instead of above – GayashanK Apr 04 '17 at 10:34
  • Bro @Sasquatch, I got error Cannot convert value of type ‘UITableViewHeaderFooterView?’ to type “PlanTableCell” in coercion. – May Phyu Apr 04 '17 at 10:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139838/discussion-between-sasquatch-and-may-phyu). – GayashanK Apr 04 '17 at 10:38
  • Yes. Can you send me hi ? I never used to chat this before. that's why bro. – May Phyu Apr 04 '17 at 10:44