ios - 如何快速制作多行表格视图标题?
问题描述
我对表格视图中的多行标题有疑问。我已经尝试过了lineBreakMode = .byWordWrapping
,numberOfLines = 0
我也设置了
tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 44
但是这些解决方案对我没有帮助。如何为我的表格视图制作多行标题?
这是我的标题视图代码:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 100))
// code for adding centered title
headerView.backgroundColor = UIColor(red: 225/255.0, green: 225/255.0, blue: 225/255.0, alpha: 1.0)
let headerLabel = UILabel(frame: CGRect(x: 10, y: 8, width: tableView.bounds.size.width - 120, height: 28))
headerLabel.textColor = UIColor.black
headerLabel.lineBreakMode = .byWordWrapping
headerLabel.numberOfLines = 0
headerLabel.text = headerTitleForSecZero
headerLabel.font = UIFont.boldSystemFont(ofSize: 14)
headerLabel.textAlignment = .left
headerView.addSubview(headerLabel)
button.frame = CGRect(x:headerView.frame.size.width - 85, y:8, width:70, height:28)
button.setTitle(buttonLabel,for: .normal)
button.tag = 1
button.addTarget(self, action: #selector(handleExpandCloseForAlim(sender:)), for: .touchUpInside)
adresDegistirmeButonu.frame = CGRect(x:headerView.frame.size.width - 100, y:8, width:90, height:28)
headerView.addSubview(adresDegistirmeButonu)
headerView.addSubview(button)
return headerView
}else{
return nil
}
}
解决方案
正如我在评论中提到的,使用允许文本标签增长的 AutoLayout 约束构建标题视图应该会给您所需的灵活性。下面的示例只是使用了一个多行文本的虚拟数组来填充标题视图。它还将页脚设置为不显示,因为空页脚会混淆布局。
class MyVC1: UIViewController {
var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
addTableView()
}
func addTableView() {
tableView = UITableView(frame: .zero, style: .grouped)
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
}
extension MyVC1: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
5
}
func numberOfSections(in tableView: UITableView) -> Int {
3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Section: \(indexPath.section), Row: \(indexPath.row)"
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//set up dummy header text
var text = [String]()
text.append( "One Line")
text.append("""
Three Lines
Three Lines
Three Lines
""")
text.append("""
Two Lines
Two Lines
""")
let header = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 100))
let label = UILabel()
label.numberOfLines = 0
let button = UIButton(type: .custom)
button.setTitle("Press Me", for: .normal)
button.setTitleColor(.purple, for: .normal)
button.backgroundColor = .lightGray
header.addSubview(label)
header.addSubview(button)
label.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: header.topAnchor, constant: 10),
label.bottomAnchor.constraint(equalTo: header.bottomAnchor, constant: -10),
label.leadingAnchor.constraint(equalTo: header.leadingAnchor, constant: 10),
button.centerYAnchor.constraint(equalTo: label.centerYAnchor),
button.widthAnchor.constraint(equalToConstant: 150),
button.trailingAnchor.constraint(equalTo: header.trailingAnchor, constant: -10),
label.trailingAnchor.constraint(greaterThanOrEqualTo: button.leadingAnchor, constant: -50)
])
label.text = text[section]
return header
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
nil
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
0
}
}
推荐阅读
- if-statement - Problems with script that unzips multiple zipped tar files then untars the same files in C shell
- python - How to specify the size of points in matplotlib point size legend
- python - 是否不可能在 aws(amazon-web-service) ubuntu18.04 实例上使用剪贴板?
- python - 部分匹配,追加和分解
- python - 试图从文件中抓取并将抓取的内容写入另一个文件?
- html - 如何防止图像溢出内联块容器
- azure - 在 Azure 应用程序的管理员同意期间获取访问令牌
- firebase - Xamarin AndroidX RecyclerView 适配器
- google-cloud-firestore - 在 Firestore 中运行维护更新的最佳做法?
- javascript - Gatsby 查询在 graphql 编辑器中有效,但在反应代码中无效