UITable, Section with Image Dynamically Swift

0

I have the following table that up to now looks like this:

What you see here are the titles of the sections, with the accordion effect, what I would like to be able to put an image next to each title, to have an effect like this

Investigating I saw that I can dynamically in the method

viewForHeaderInSection

add this image my code so far is

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    view.backgroundColor = UIColor.white
    view.addBottomBorder(Utils.Color.COLOR_SEPARACIONES, height: 2.5, separation: 0)
    let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapOnHeader(_:)))
    tap.numberOfTapsRequired = 1
    view.tag = section
    view.addGestureRecognizer(tap)

    let label : UILabel = UILabel()
    label.text = sectionTitles[section]
    label.frame = CGRect(x: 20, y: 5, width: 200, height: 30)
    label.textColor = Utils.Color.COLOR_2B5034
    view.addSubview(label)

    return view
}

I am not sure how I can dynamically create an image and add the contrains so that it is centered with respect to the label, thank you already, I use swift 4

    
asked by Bruno Sosa Fast Tag 07.05.2018 в 20:33
source

1 answer

1

You have to create a UIImageView and add it as subview of variable view .

In the func tableView(_:viewForHeaderInSection:) method you have to add:

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) // el frame seguramente varíe...
imageView.image = UIImage(...) // ver desde donde se obtiene la imagen
view.addSubview(imageView)
    
answered by 07.05.2018 / 20:44
source