Swift Table 查看单元格值垂直显示失败
Swift Table View Cell Value Failed to display vertically
我是 swift 的新手。我正在使用子标题以编程方式创建 table 视图单元格。我定义了两个标签和两个函数来获取值形式 API 并将其显示到标签控件中,但问题是标签的值显示到正确的位置。我想显示标题,然后在标题下方是子标题值。这是屏幕截图。
这是代码 table 查看单元格。
extension ViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel.rovers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: StoryCell.identifier , for: indexPath) as? StoryCell
else{ return UITableViewCell()}
let row = indexPath.row
let title = viewModel.getTitle(by: row)
cell.configureCell(title:title)
let Id = viewModel.getId(by: row)
cell.configureCell(Id: Id)
return cell
}
}
这是配置标签的代码。
import UIKit
class StoryCell: UITableViewCell {
static let identifier = "StoryCell"
private lazy var storyTitleLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.textAlignment = .left
return label
}()
private lazy var storyIdLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.textAlignment = .left
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpUI()
setUpUIID()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configureCell(title: String) {
storyTitleLabel.text = "Status :\(title)"
}
func configureCell(Id: Int) {
storyIdLabel.text = "Id: \(String(Id))"
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
private func setUpUI() {
contentView.addSubview(storyTitleLabel)
// constraints
let safeArea = contentView.safeAreaLayoutGuide
storyTitleLabel.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
storyTitleLabel.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
storyTitleLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
storyTitleLabel.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
}
private func setUpUIID() {
contentView.addSubview(storyIdLabel)
// constraints
let safeArea = contentView.safeAreaLayoutGuide
storyIdLabel.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
storyIdLabel.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
storyIdLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
storyIdLabel.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
}
}
我在下面对您的代码检查进行了一些更改。现在应该没问题了。
private func setUpUI() {
contentView.addSubview(storyTitleLabel)
// constraints
let safeArea = contentView.safeAreaLayoutGuide
storyTitleLabel.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
storyTitleLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
storyTitleLabel.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
}
private func setUpUIID() {
contentView.addSubview(storyIdLabel)
let safeArea = contentView.safeAreaLayoutGuide
storyIdLabel.topAnchor.constraint(equalTo: storyTitleLabel.topAnchor).constant = 5
storyIdLabel.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
storyIdLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
storyIdLabel.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
}
我是 swift 的新手。我正在使用子标题以编程方式创建 table 视图单元格。我定义了两个标签和两个函数来获取值形式 API 并将其显示到标签控件中,但问题是标签的值显示到正确的位置。我想显示标题,然后在标题下方是子标题值。这是屏幕截图。
这是代码 table 查看单元格。
extension ViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel.rovers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: StoryCell.identifier , for: indexPath) as? StoryCell
else{ return UITableViewCell()}
let row = indexPath.row
let title = viewModel.getTitle(by: row)
cell.configureCell(title:title)
let Id = viewModel.getId(by: row)
cell.configureCell(Id: Id)
return cell
}
}
这是配置标签的代码。
import UIKit
class StoryCell: UITableViewCell {
static let identifier = "StoryCell"
private lazy var storyTitleLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.textAlignment = .left
return label
}()
private lazy var storyIdLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.textAlignment = .left
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpUI()
setUpUIID()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configureCell(title: String) {
storyTitleLabel.text = "Status :\(title)"
}
func configureCell(Id: Int) {
storyIdLabel.text = "Id: \(String(Id))"
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
private func setUpUI() {
contentView.addSubview(storyTitleLabel)
// constraints
let safeArea = contentView.safeAreaLayoutGuide
storyTitleLabel.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
storyTitleLabel.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
storyTitleLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
storyTitleLabel.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
}
private func setUpUIID() {
contentView.addSubview(storyIdLabel)
// constraints
let safeArea = contentView.safeAreaLayoutGuide
storyIdLabel.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
storyIdLabel.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
storyIdLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
storyIdLabel.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
}
}
我在下面对您的代码检查进行了一些更改。现在应该没问题了。
private func setUpUI() {
contentView.addSubview(storyTitleLabel)
// constraints
let safeArea = contentView.safeAreaLayoutGuide
storyTitleLabel.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
storyTitleLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
storyTitleLabel.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
}
private func setUpUIID() {
contentView.addSubview(storyIdLabel)
let safeArea = contentView.safeAreaLayoutGuide
storyIdLabel.topAnchor.constraint(equalTo: storyTitleLabel.topAnchor).constant = 5
storyIdLabel.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
storyIdLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
storyIdLabel.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
}