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?
The customary NIB process is:
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")
}
Define your custom cell class:
import UIKit
class NameInput: UITableViewCell {
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
}
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.
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.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With