将两个表视图合并到一个视图控制器中?

问题描述 投票:0回答:1

我想创建一个新部分,但没有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

swift tableview viewcontroller
1个回答
0
投票
方法1:将UI元素放置在水平stackView

您可以在水平stackView中同时添加imageViewtitleLabel并对其施加约束。如果没有imageContent,则标签将自身调整到起始位置,前提是imageView没有widthAnchor。这是一种更简单的方法

方法2:如果您不想在StackView中放置元素

删除与titleLabel相关的所有尾随锚/约束。只需添加相对于imageView的前导约束,而不添加任何widthAnchor即可。

[titleLabel将在没有imageView内容时将其自身对齐到imageView的开始位置(imageView的宽度将为0]
© www.soinside.com 2019 - 2024. All rights reserved.