1

I want to create a new section but without imageView, what do I need to do? Because if I let the string blank (nil not accepted) the titleLabel is going to be aligned with the titleLabel in the first section. I want to make the title in the second section align 20 from the left (with no image) Do I have to create another model for the sections I want with no Image? Can I add a new section without Image inside cellForRowAt??

fileprivate var CELL_ID = "CELL_ID"


fileprivate var aerealCell: [[AerealCellModel]] = [
    [
        AerealCellModel(image: "aereal_icon", title: "Aereal", arrow: "chevron.right")
    ],
    [
        AerealCellModel(image: " ", title: "Indice de Masa Corporal", arrow: "chevron.right"),
        AerealCellModel(image: " ", title: "Clasificación ASA", arrow: "chevron.right"),
        AerealCellModel(image: " ", title: "Riesgo Cardiaco de Gupta", arrow: "chevron.right"),
        AerealCellModel(image: " ", title: "Escala de Riesgo de Lee", arrow: "chevron.right"),
        AerealCellModel(image: " ", title: "Peso Ideal", arrow: "chevron.right"),
        AerealCellModel(image: " ", title: "Tamaño de Tubo Pediátrico", arrow: "chevron.right"),
        AerealCellModel(image: " ", title: "Clasificacion de Mallampati", arrow: "chevron.right")
    ]

]

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor  = .systemBackground

    navigationController?.navigationBar.prefersLargeTitles  = true
    setupTableView()

}

fileprivate func setupTableView() {
    tableView.register(AerealCell.self, forCellReuseIdentifier: CELL_ID)
}



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return aerealCell[section].count
}


override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 54
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath) as! AerealCell
    let aerealMainCell = aerealCell[indexPath.section][indexPath.row]


    cell.aerealCalcCell = aerealMainCell
    return cell
}


override func numberOfSections(in tableView: UITableView) -> Int {
    return aerealCell.count
}


override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    let label = UILabel()
    label.text = section == 0 ? "CALCULADORA PRINCIPAL" : "OTRAS CALCULADORAS    (Premium Members)"
    label.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(label)
    label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
    label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
    label.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -5).isActive = true
    label.heightAnchor.constraint(equalToConstant: 30).isActive = true
    label.numberOfLines = 1
    label.adjustsFontSizeToFitWidth = true
    view.backgroundColor = .systemGray6
    label.font = UIFont.systemFont(ofSize: 14, weight: .light)

    return view
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 54
}}

enter image description here

2 Answers2

0

Approach 1: Place your UI elements in a horizontal stackView

You can add both the imageView and titleLabel in a horizontal stackView and apply constraints to it. When there is no imageContent the label adjusts itself to the start position given that there is no widthAnchor to the imageView. This is a much simpler approach

Approach 2: If you don't want to place elements in StackView

Remove any trailing anchor/constraint associated with the titleLabel. Just add a leading constraint relative to your imageView and don't add any widthAnchor to it.

The titleLabel will align itself to the imageView start position when there is no imageView content(imageView width will be 0)

Vikram Parimi
  • 777
  • 6
  • 29
  • I think those option are not an option. imageView is a String type in the model and anytime I left "" blank, constraints will remain for the absent image. so Im stuck. – Herman Granados MD Apr 26 '20 at 20:56
0
  1. Set width constraint to image view in cell.
  2. Whenever you don’t want to display image, set image width constraint to 0.
PrajaktaJ
  • 66
  • 4