Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell Subclass with XIB Swift

I have a UITableViewCell subclass NameInput that connects to an xib with a custom init method.

class NameInput: UITableViewCell {

    class func make(label: String, placeholder: String) -> NameInput {

        let input = NSBundle.mainBundle().loadNibNamed("NameInput", owner: nil, options: nil)[0] as NameInput

        input.label.text = label
        input.valueField.placeholder = placeholder
        input.valueField.autocapitalizationType = .Words

        return input
    }

}

Is there a way I can initialize this cell in the viewDidLoad method and still reuse it? Or do I have to register the class itself with a reuse identifier?

like image 787
jamespick Avatar asked Feb 12 '15 23:02

jamespick


2 Answers

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.

like image 60
Rob Avatar answered Nov 08 '22 04:11

Rob


You don't initialize cells in viewDidLoad. You should register the XIB, not the class, with your table view. You should set up the label and text field in tableView:cellForRowAtIndexPath: (possibly by calling an instance method on NameInput).

like image 39
rob mayoff Avatar answered Nov 08 '22 04:11

rob mayoff