首页 > 解决方案 > 如何将不同的字符串值放入 UITableView 中的多行

问题描述

我有 UITableView 并在其中尝试显示两个单独的字符串值,每个值在不同的行上。第二个字符串或金额应显示在表格视图中的第一行文本下方。

下面的代码显示了表格视图中的文本是如何受到约束的,以及我假设将值分成不同行的功能将成为问题的其他功能:

    func set(bio: Bio){
    bioLabel.text = bio.statistics
    amountLabel.text = bio.amount
}

func configureBioLabel(){
    bioLabel.numberOfLines = 0
    bioLabel.textColor  = .systemGreen
    bioLabel.font = UIFont(name: "Seravek", size: 22)
    bioLabel.adjustsFontSizeToFitWidth = true
    
    
}

func setBioLabelConstraints(){
    bioLabel.translatesAutoresizingMaskIntoConstraints = false
    bioLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    bioLabel.anchor(top: nil, leading: leadingAnchor, bottom: nil, trailing: nil, padding: .init(top: 0, left: 50, bottom: 0, right: 0))
    bioLabel.heightAnchor.constraint(equalToConstant: 80).isActive = true
    bioLabel.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 16/9).isActive = true
}

func configureAmount(){
    amountLabel.numberOfLines = 0
    amountLabel.textColor = .systemYellow
    amountLabel.font = UIFont(name: "Seravek", size: 22)
    amountLabel.adjustsFontSizeToFitWidth = true
}

func setAmountLabelConstriants(){
    amountLabel.translatesAutoresizingMaskIntoConstraints = false
    amountLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    amountLabel.anchor(top: nil, leading: leadingAnchor, bottom: nil, trailing: nil, padding: .init(top: 0, left: 50, bottom: 0, right: 0))
    amountLabel.heightAnchor.constraint(equalToConstant: 80).isActive = true
    amountLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0).isActive = true
}

然后在这个扩展中,单独的 sting 值被添加到表视图中。

    extension BioInfo: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return bio.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: BioCellId.bioCellId) as! CustomBioCell
    let bios = bio[indexPath.row]
    cell.set(bio: bios)
    return cell
}


}

struct Bio{
var statistics: String
var amount: String
}

extension BioInfo{

func fetchData() -> [Bio]{

    let info = Bio(statistics: "Text", amount: "0")
    
    let info2 = Bio(statistics: "Text", amount: "0")
    
    let info3 = Bio(statistics: "Text", amount: "0")
    
    return [info, info2, info3]
    
}
}

我的表格视图单元格在这里定义:

    class BioInfo: UICollectionViewCell{
lazy var tableView: UITableView = {
         let tblView = UITableView()
         tblView.delegate = self
         tblView.dataSource = self
         tblView.translatesAutoresizingMaskIntoConstraints = false
         return tblView
   }()

var bio: [Bio] = []



struct BioCellId {
    static let bioCellId = "CustomBioCell"
}



override init(frame: CGRect) {
    super.init(frame: frame)
    

    bio = fetchData()
    setupTableView()

}
func setupTableView() {

    addSubview(tableView)
    translatesAutoresizingMaskIntoConstraints = false
    tableView.anchor(top: topAnchor, leading: leadingAnchor, bottom: nil, trailing: trailingAnchor, padding: .init(top: 350, left: 0, bottom: 300, right: 0),size: .init(width: frame.width, height: 300))
    
    
    tableView.rowHeight = 100
    
    tableView.register(CustomBioCell.self, forCellReuseIdentifier: BioCellId.bioCellId)


}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

标签: iosswiftxcodeuitableview

解决方案


推荐阅读