如何在 swift ios 中的自定义 TableView 单元格中向多个标签显示多个字符串值?

How can I display multiple string values to multiple labels in a custom TableView Cell in swift ios?

var leadername = ["1","2","3","4"]

var districts = ["Delhi","Kerala"]

override func viewDidLoad() {

    leadTableSetup()
    super.viewDidLoad()
}

func leadTableSetup(){

    LeadTableView.delegate = self
    LeadTableView.dataSource = self

    self.LeadTableView.register(UINib(nibName: "LeaderBoardTableViewCell", bundle: nil), forCellReuseIdentifier: "leadCell")   
}

func numberOfSections(in tableView: UITableView) -> Int {

    return 5
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 14

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "leadCell") as! LeaderBoardTableViewCell
    // Set text from the data model
    cell.areaLbl.text = districts[indexPath.row]
    cell.leaderNameLbl.text = leadername[indexPath.row]

    return cell
}

我已经声明了两个字符串,我需要在我创建的自定义集合视图单元格的标签中显示这些字符串。我怎样才能做到这一点?我需要在一个标签中显示 "leadername" 字符串,在另一个标签中显示 "districts" 标签。

继续这个演示,Shared Demo

演示结束后,如果您仍然遇到任何问题,请告诉我。

现在听这里

我想你需要这样的输出,

执行步骤:-

  1. 在你的故事板中创建一个新的 viewcontroller(比如,CustomTableVC)和一个 UITableView(给它自己的 class 提供约束和委托),取UItableView 的出口(说,tblMyCustom)

  2. 现在按 CLT+N 创建新文件并像下面的图像一样执行操作,Subclass - UItableViewCell 并勾选 XIB 选项。

  1. 打开我们的 xib 文件,添加新的 UIView(说是 myView,如下图高亮所示),在这个 myView 中添加两个标签

  2. 现在在它的 customCell 中取出这两个标签 class

    class CustomTableCell: UITableViewCell {

    @IBOutlet var lblLeaderNo: UILabel!
    @IBOutlet var lblDistict: UILabel!
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
        // Configure the view for the selected state
    }
    

    }

  3. 现在回到你的 Viewcontroller Class

    import UIKit
    
    class CustomTableVC: UIViewController , UITableViewDelegate, UITableViewDataSource{
    

@IBOutlet var tblMyCustom: UITableView!

var leaderno : [String]!

变量区分:[字符串]!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        self.tblMyCustom.register(UINib(nibName: "CustomTableCell", bundle: nil), forCellReuseIdentifier: "customCell")

        self.leaderno = ["1", "2", "3", "4"]
        self.distict = ["Delhi","Kerala", "Haryana", "Punjab"]
        // above both array must have same count otherwise, label text in null

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return leaderno.count;
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        var customCell: CustomTableCell! = tableView.dequeueReusableCell(withIdentifier: "customCell") as? CustomTableCell

        customCell.lblLeaderNo.text = self.leaderno[indexPath.row]
        customCell.lblDistict.text = self.distict[indexPath.row]

        return customCell
    }
}

上面都是VC的代码,单码格式这里就不放了,不知道为什么。

现在,按照这些步骤,您将获得我在程序开始时向您展示图像的输出。