Два табличных представления в одном View Controller? - PullRequest
1 голос
/ 26 апреля 2020

Я хочу создать новый раздел, но без imageView, что мне нужно сделать? Потому что, если я оставлю строку пустой (ноль не принимается), titleLabel будет выровнен с titleLabel в первом разделе. Я хочу, чтобы заголовок во втором разделе был выровнен по 20 слева (без изображения). Нужно ли создавать еще одну модель для нужных разделов без изображения? Могу ли я добавить новый раздел без изображения внутри 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

1 Ответ

0 голосов
/ 26 апреля 2020

Подход 1: Поместите элементы пользовательского интерфейса в горизонтальное представление стека

Вы можете добавить как imageView, так и titleLabel в горизонтальное представление стека и наложить на него ограничения. Когда imageContent отсутствует, метка подстраивается под начальную позицию, учитывая, что widthAnchor к imageView отсутствует. Это гораздо более простой подход

Подход 2: Если вы не хотите помещать элементы в StackView

Удалите все конечные привязки / ограничения, связанные с titleLabel , Просто добавьте ведущее ограничение относительно вашего imageView и не добавляйте widthAnchor к нему.

TitleLabel будет выравниваться по начальной позиции imageView, когда нет содержимого imageView (ширина imageView будет 0)

...