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

时间:2016-12-18 06:48:06

标签: ios uicollectionview swift3 uicollectionviewcell

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”字符串,在另一个标签中显示“区域”标签。

1 个答案:

答案 0 :(得分:0)

使用此演示Shared Demo

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

现在听这里

我认为你需要这样的输出, Expected To your need

按照以下步骤操作: -

  1. 在故事板中创建一个新的viewcontroller(说明,CustomTableVC)和一个UITableView(给出约束并委托给自己的类),取出UItableView的出口(说,tblMyCustom)

  2. 现在按CLT + N获取新文件,并在下面的图像,子类 - UItableViewCell中执行此操作,并在XIB选项上打勾。

  3. enter image description here

    1. 打开我们的xib文件,添加新的UIView(如myView所示,如下图所示),在此myView中添加两个标签 Xib cell

    2. 现在在customCell类

      中输出这两个标签

      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类

      import UIKit
      
      class CustomTableVC: UIViewController , UITableViewDelegate, UITableViewDataSource{
      
    4. @IBOutlet var tblMyCustom:UITableView!

      var leaderno:[String]!

      var distict:[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的代码,它没有以单代码格式在这里定居,我不知道为什么。

      现在,按照以下步骤输出,我会在程序开始时显示图像。