Uitableviewcell Subclass with Xib Swift

UITableViewCell Subclass with XIB Swift

The customary NIB process is:

  1. Register your NIB with the reuse identifier. In Swift 3:

    override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell")
    }

    In Swift 2:

    override func viewDidLoad() {
    super.viewDidLoad()

    tableView.registerNib(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell")
    }
  2. Define your custom cell class:

    import UIKit

    class NameInput: UITableViewCell {

    @IBOutlet weak var firstNameLabel: UILabel!
    @IBOutlet weak var lastNameLabel: UILabel!

    }
  3. Create a NIB file in Interface Builder (with the same name referenced in step 1):

    • Specify the base class of the tableview cell in the NIB to reference your custom cell class (defined in step 2).

    • Hook up references between the controls in the cell in the NIB to the @IBOutlet references in the custom cell class.

  4. Your cellForRowAtIndexPath would then instantiate the cell and set the labels. In Swift 3:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NameInput

    let person = people[indexPath.row]
    cell.firstNameLabel.text = person.firstName
    cell.lastNameLabel.text = person.lastName

    return cell
    }

    In Swift 2:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NameInput

    let person = people[indexPath.row]
    cell.firstNameLabel.text = person.firstName
    cell.lastNameLabel.text = person.lastName

    return cell
    }

I wasn't entirely sure from your example what controls you placed on your cell, but the above has two UILabel controls. Hook up whatever @IBOutlet references make sense for your app.

Linking multiple custom UITableViewCell's to single xib

Add view loaded by xib to UITableViewCell classes in which you want to use it.

  1. Create your xib as per your require design, in your example PickerTableViewCell.xib

  2. Create UITableViewCell sub-classes in which you want to use that view. I am using FirstTableViewCell & SecondTableViewCell for this.

in constructor of table cell load the xib and add it to table cell.

let nib = Bundle.main.loadNibNamed("PickerTableViewCell", owner: nil, options: nil)
if let view = nib?.first as? UIView{
self.addSubview(view)
}

if xib have any @IBOutlet then get them by viewWithTag function and assign to class variables

if let label = self.viewWithTag(1) as? UILabel{
self.label = label
}

override reuseIdentifier var of each tableviewCell subclass with different name

override var reuseIdentifier: String?{
return "FirstTableViewCell"
}

Now You can use these classes where you want, for using this follow below steps:

register this tableviewCell subclass with xib with tableview:

tableView.register(FirstTableViewCell.self, forCellReuseIdentifier:"PickerTableViewCell")

now in cellForRowAt indexPath method use it.

var cell = tableView.dequeueReusableCell(withIdentifier: "FirstTableViewCell", for: indexPath) as? FirstTableViewCell
if cell == nil {
cell = FirstTableViewCell()
}
cell?.label?.text = "FirstTableViewCell"

Multiple UITableViewCell subclasses using same xib?

Use composition rather than inheritance. Keep the 1-1 relationship between your BaseCell class and it's cell in the nib, add a property for an implementation object which you create and add to the cell object after you dequeue it.

Creating single .xib for different UITableViewCell classes

You can, but, instead of using the UITableViewCell, use similar UIView

Create a xib for the cell content view, give it a class (like CellView).
In each cell class, get the CellView with loadNibNamed and set it as the cell content view, then you can freely change the property of the CellView and use it as other cell class's content view

Maybe there's better solution, but this is how i do it for similar looking UIViewController, you might also try cell xib without class set in storyboard and try to load it and see if the error still persist or not

Trying to subclass UITableViewCell on Swift

Without an error message or much code to go by, what would happen if you declared your cell variable as optional? Would it crash then? Also, shouldn't you use dequeueReusableCellWithIdentifier(...) to get a reference to your cell?



Related Topics



Leave a reply



Submit