0

I have about 3 or 4 table controllers that will all use the same tableview cell. I keep going back on fourth of the possible logic. Can I use the same tableViewCell in multiple tableView Controllers assuming the information is between the two is the same? Or will I have to create a new cell for each controller?

jonpeter
  • 599
  • 8
  • 28

2 Answers2

4

Yes you can.

I am assuming that you are using Swift.

Goto File -> New and select cocoaTouch class as follows.

enter image description here

Now name Your class for custom cell and make it subClass of UITableViewCell. also check the box which says "Also create Xib file"

enter image description here

Now design your cell in this Xib and create outlets in its .Swift file. Lets say you have a custom tableView cell which looks something like this

enter image description here

Which contains a label or ImageView or anyThing that you have in your cell. Now in your swift file of custom cell you can write a method like so

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell

return cell
}

Now you can use this cell in any tableView in your application just like below

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor()
// do something with your cell

}

I hope it helps.

Update for Swift 3 and Swift 4:

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> YourCustomTableViewCell {
    let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
    tableView.register(UINib(nibName: "YourCustomTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCell(withIdentifier: kYourCustomTableViewCellIdentifier, for: indexPath) as! YourCustomTableViewCell

    return cell
}
Umair Afzal
  • 4,947
  • 5
  • 25
  • 50
  • Very helpful! I am wondering though, how would I go about initially connecting my IBOutlets tho? Would I simply choose one storyboard cell to connect to? – jonpeter Oct 28 '16 at 01:47
  • @jonpeter Outlets of what ? If your are talking connecting your cell to your tableView then no need to do that. You just write the above code and it will work. And if you are asking for ouutlets of your components to your cell, then I will update my answer for that as well. – Umair Afzal Oct 28 '16 at 04:25
  • Sorry for the delayed response. I was wondering if/how I would go about connecting the label in my cell to the tablecell class – jonpeter Nov 01 '16 at 00:17
  • Well, just open your Xib of custom class and open assistant editor and create outlets just like your create on your viewController's class – Umair Afzal Nov 01 '16 at 04:15
0

Yes you can use the table view cell in multiple tableView Controllers .

Sanjukta
  • 1,057
  • 6
  • 16