4

In my app I am using a custom tableViewCell using a xib file. I create the .xib file outlet the labels etc to my TableViewCell class

In my ViewController the code I used to populate and show the cell on the table view is as follows:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let transaction = statementArray[indexPath.row]
    let cell = Bundle.main.loadNibNamed("StatementCell", owner: self, options: nil)?.first as! StatementCell
    
    
    
    cell.transAmount.text = String(transaction.transAmount)
    cell.transDesc.text = transaction.transDesc
    cell.transFees.text = String(transaction.transFees)
    
    return cell
}

I am aware that the way tableViews work is that they reuse the cell that goes off the screen. Is the way i am loading the .xib and populating the cell correct? Or do I have to add something to my code?

Vyankatesh
  • 139
  • 1
  • 10
pavlos
  • 547
  • 1
  • 9
  • 23
  • Is there a specific reason to use a .xib? You can create multiple cells in the same table view in Interface Builder. – vadian Apr 01 '17 at 08:30
  • I am using a .xib because i will be using different cells depending on the data i have – pavlos Apr 01 '17 at 08:38
  • Where is the problem? You can create as many cells as you like in the same table view with different UI elements, sizes etc. Just drag cells into the table view and assign an identifier and (optional) a class. It's much more convenient than using extra .xibs. – vadian Apr 01 '17 at 08:41
  • @vadian may be Xib is more handy for reusing the cell in multiple tableViews ? – Umair Afzal Oct 12 '17 at 05:42
  • @pavlos please have a loot at https://stackoverflow.com/questions/40275727/is-it-possible-to-create-one-tableviewcell-that-can-be-used-in-multiple-table-co/40277758#40277758 – Umair Afzal Oct 12 '17 at 05:44
  • 1
    @UmairAfzal Yes in this case a XIB is more handy but the question doesn't seem to be about multiple table views. – vadian Oct 12 '17 at 05:45

3 Answers3

4

First you need to register your custom cell with UITableView

yourTableView.register(UINib(nibName: "StatementCell", bundle: nil), forCellReuseIdentifier: "cellIdentifier")

then in UITableView Delegate method cellForRowAt you need to write

let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! StatementCell
cell.textLabel?.text = "Sample Project"
return cell

now you can access your UITableViewCell properties with cell.propertyName

and you must take care that "cellIdentifier" and "forCellReuseIdentifier" both value must be same.

devang bhatt
  • 460
  • 2
  • 15
0

One more thing is you need to register your class for reuse identifier in viewDidLoad method.

YourTable.registerclass(tablecell, forCellReuseIdentifier: "identifier"

And make sure you have connect all the required outlets.

Gourav Joshi
  • 2,419
  • 2
  • 27
  • 45
0

FIrst Register Custom cell nibCalss in tableview

let str1 : NSString = "StatementCell"

tableView.register(UINib(nibName: "StatementCell", bundle: nil), forCellReuseIdentifier: str1 as String)

Now initialise tableviewcell

let cell1:StatementCell = tableView.dequeueReusableCell(withIdentifier: str1 as String) as! StatementCell!

Now access tableviewcell outlet collection.

Sakir Sherasiya
  • 1,562
  • 1
  • 17
  • 31