ios - 以编程方式 UITableView 中的自动调整标题和单元格
问题描述
我以编程方式创建了 UIViewTable 我通过扩展自定义了标题和单元格外观。我所需要的只是使标题/单元格中显示的大量文本可以通过以下方式查看:
lineBreakMode = NSLineBreakMode.byWordWrapping // enable multi line numberOfLines = 0 // for Automatic size
我几乎使用了所有东西,但没有任何效果。
我用了:
self.tableView.estimatedRowHeight = 200.0 self.tableView.rowHeight = UITableView.automaticDimension
我放:
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return 100 }
我也做了:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension }
似乎没有任何效果
这是我的扩展:
扩展索引控制器 {
override func numberOfSections(in tableView: UITableView) -> Int { return sections.count } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let subDatas = sections[section].sub_catigories // [1] return subDatas?.count ?? 0 } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let currentSection = sections[indexPath.section] let currentSubdata = currentSection.sub_catigories?[indexPath.row] //print(currentSubdata!.id) let vc = indexControllerTwo() vc.catNumber = currentSubdata!.id vc.sectionTitle = currentSubdata?.name navigationController?.pushViewController(vc, animated: true) } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath) // [2] let currentSection = sections[indexPath.section] let currentSubdata = currentSection.sub_catigories![indexPath.row] // use listCell guard let titleCell = cell as? listCell else { return cell } titleCell.titleLabel.text = currentSubdata.name titleCell.listCount.text = "\(currentSubdata.number_of_subcatigories ?? 0)" // titleCell.titleLabel.numberOfLines = 3 // titleCell.titleLabel.lineBreakMode = NSLineBreakMode.byWordWrapping // titleCell.titleLabel.baselineAdjustment = .alignCenters // titleCell.titleLabel.adjustsFontSizeToFitWidth = true // self.tableView.estimatedRowHeight = 200.0 // self.tableView.rowHeight = UITableView.automaticDimension cell.layer.backgroundColor = UIColor.clear.cgColor return cell }
请注意: listCell 仅用于自定义和约束,这里是:
import UIKit class listCell: UITableViewCell { var safeArea: UILayoutGuide! let imageCell = UIImageView() let titleLabel = UILabel() let subTitleLabel = UILabel() let listCount = UILabel() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setupView() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setupView(){ safeArea = layoutMarginsGuide setupTitleLabel() setupListCount() } func setupTitleLabel(){ addSubview(titleLabel) titleLabel.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ titleLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor), titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 7) ]) titleLabel.font = UIFont(name: "verdana-Bold", size: 16) } func setupListCount(){ addSubview(listCount) listCount.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ listCount.leadingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -30), listCount.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: -11) ]) listCount.font = UIFont(name: "verdana", size: 10) } }
请帮助我使标题和单元格文本字段自动调整大小。谢谢你的时间。
解决方案
推荐阅读
- python - Pandas txt 到 csv 输出只显示前两行值,我如何获得完整的数据来显示?
- python - 搜索数据框时 loc 是可选属性吗?
- sql-server - Visual Studio 2019 缺少 SQL CLR VB
- asp.net-core - 由于@page {id} 类型,无法打开剃须刀页面
- postgresql-12 - 致命:抱歉,postgresql-12 中已经有太多客户端,我的连接是否已经正确关闭?
- css - CSS 自定义属性(变量)继承覆盖和取消设置
- reporting-services - 在 RDLC 报告结果中搜索数据时,未将对象引用设置为对象的实例
- html - 数字在 Div 中失真
- reactjs - 如何在 React JS 中使字符串的特定部分变为粗体?
- javascript - 不和谐的警报命令。date.hours() 中的问题