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
}

I have declared two strings and I need to display these strings in the labels in my custom collection view cell that I have created. How can I achieve this? I need to display "leadername" string in one label and "districts" label in another label.

Umair Afzal
  • 4,947
  • 5
  • 25
  • 50
Rakesh
  • 131
  • 2
  • 6
  • 14
  • can you show `LeaderBoardTableViewCell` – Adrian Bobrowski Dec 18 '16 at 07:00
  • [Sample](https://www.credera.com/wp-content/uploads/2015/08/Custom-Collection-Views-1.png) I think you need output something like this. Two label in each cell , but there values is accroding to the array value. Right? @Rakesh – dahiya_boy Dec 18 '16 at 07:00
  • @TinuDahiya. Exactly – Rakesh Dec 18 '16 at 07:05
  • @AdrianBobrowski class LeaderBoardTableViewCell: UITableViewCell { IBOutlet weak var approvalLabel: UILabel! IBOutlet weak var areaLbl: UILabel! IBOutlet weak var leaderNameLbl: 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 } – Rakesh Dec 18 '16 at 07:05
  • @Rakesh please edit your question to past `LeaderBoardTableViewCell` code, and change tags from uicollectionviewcell to uitableviewcell – Adrian Bobrowski Dec 18 '16 at 07:08
  • @Rakesh you need to display these texts in the same cell or in different – Adrian Bobrowski Dec 18 '16 at 07:11
  • @Rakesh Now, I edited my answer according to UITableView and my last answer is according to the UICollectionView. Check it out – dahiya_boy Dec 18 '16 at 07:18
  • Go with this demo, and try to study some basics about this i.e. How tableView works and how you maintain the UITableViewCell either custom or fromXib. Because you can do lots of stuff with tableView. so my suggestion is first learn basics otherwise you always stuck. [Shared demo](http://stackoverflow.com/questions/25541786/custom-uitableviewcell-from-nib-in-swift) After the demo, If you still face any problem then let me know. – dahiya_boy Dec 18 '16 at 07:04
  • @TinuDahiya. I missed it. Can you once again edit it please? – Rakesh Dec 18 '16 at 14:25
  • @AdrianBobrowski I have to display all the array values in the cell. – Rakesh Dec 18 '16 at 14:25
  • @Rakesh I shared a demo. **Suggestion for you :** Try to study some basics about UITableView i.e. How UITableView works and how to you maintain the UITableViewCell either custom or from Xib. Because you can do lots of stuff with tableView. so my suggestion is first learn basics otherwise you always stuck. If you still have any doubt then ask freely, but make a habit to search answer rather then posting, it improves your skills. – dahiya_boy Dec 18 '16 at 16:46
  • @TinuDahiya Thanks a lot for the demo that you have shared. I am quite familiar with the tableview properties. But I am not able to understand how to display multiple strings in the cell. I have checked for the references but I am not getting what I really need. Please help – Rakesh Dec 19 '16 at 04:13
  • What output you are expecting and what output you are getting, share screen shot with little description then i will check it out, dont worry. – dahiya_boy Dec 19 '16 at 04:41
  • @TinuDahiya See I have to array strings "Leadername" and "districts". I have to display Leadername to leaderName label in the cell and districts to areaLabel in the cell. – Rakesh Dec 19 '16 at 04:54

1 Answers1

0

Go with this demo, Shared Demo

After the demo, If you still face any problem then let me know.

Now Listen Here

I think you need output something like this, Expected To your need

Follow the steps: -

  1. Create a new viewcontroller(says, CustomTableVC) in your storyboard and one UITableView(give constraints and delegate to its own class), take outlet of UItableView (says, tblMyCustom)

  2. Now press CLT+N for new file and do like this below image, Subclass - UItableViewCell and also tick on XIB option.

enter image description here

  1. Open our xib file, add new UIView (says myView, as you see highted in below image), in this myView add two labels Xib cell

  2. Now take outlet of these two labels in its 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. Now back to your Viewcontroller Class

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

above all is code of VC, it is not getting settle down here in single code format, I dont know why.

Now, follow these steps you get output as i show you image in starting of the procedure.

Community
  • 1
  • 1
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • Then how do you attach outlets to a table view cell when you designed your custom cell in storyboards. I am have the same problem here, and the solution would work for me, however I am unable to attach the different outlets to my table view cell (which is a separate document from the tableview controller). – LEKYSMA May 10 '20 at 13:42
  • @LEKYSMA , You attach outlets in cell file as same like you do. Nothing different. You can also achieve same result w/o XIB . Still have issue then specify exact issue point for better assistance. – dahiya_boy May 10 '20 at 21:30