2016-12-18 23 views
1
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”字符串,在另一个标签中显示“区域”标签。如何在Swift ios中的自定义TableView Cell中将多个字符串值显示为多个标签?

+0

你可以显示'LeaderBoardTableViewCell' –

+0

[样本](https://www.credera.com/wp-content/uploads/2015/08/Custom-Collection-Views-1.png)我认为你需要输出一些东西喜欢这个。每个单元格中都有两个标签,但其中的值与数组值相符。对? @Rakesh –

+0

@TinuDahiya。正确 – Rakesh

回答

0

转到与此演示,Shared Demo

演示后,如果您仍然面临任何问题,然后让我知道。

现在在这里听

我想你需要输出这样的事情, Expected To your need

以下步骤: -

  1. 创建一个新的视图控制器(说,CustomTableVC )在你的故事板和一个UITableView(给它自己的类提供约束和委托),取出UItable的出口查看(说,tblMyCustom)

  2. 现在按CLT + N的新文件,并做下面的图像,子类 - UItableViewCell和也勾选XIB选项。

enter image description here

  • 打开我们的厦门国际银行文件,添加新的UIView(MyView的说,当你看到下面的图像highted),在此MyView的添加两个标签 Xib cell

  • 现在采取这两个标签的出口在其customCell类

    类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 
    } 
    

    }

  • 现在回到你的ViewController类

    import UIKit 
    
    class CustomTableVC: UIViewController , UITableViewDelegate, UITableViewDataSource{ 
    
  • @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的代码,它在单一代码格式中没有得到解决,我不知道为什么。

    现在,按照以下步骤获取输出结果,并在过程开始时显示图像。

    +0

    非常感谢。有效 – Rakesh

    相关问题