【发布时间】:2020-04-26 20:05:25
【问题描述】:
我想创建一个新部分但没有 imageView,我需要做什么?因为如果我让字符串为空(不接受 nil),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
}}
【问题讨论】:
-
您可以创建和使用两个不同的单元格
标签: swift tableview viewcontroller