Reputation: 3
I first built a normal custom cell with no nib and it worked perfectly. But now I need to reuse the same cell layout in various views so I'm trying to use a nib in stead of a regular prototype cell. The cell does display, with information inside. But I can't interact with it. For example, I can't write in the text field. When I click, it just selects the whole cell (although I turned off the selection of the cell in the tableview options).
I tried different ways to init the cell, but it either crashes or does nothing.
Here is my code:
CelluleView (the custom cell)
class CelluleView: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
ViewController
class PersoViewController: UITableViewController {
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
}
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "CelluleView", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "Cellule")
tableView.dataSource = self
tableView.delegate = self
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellule = tableView.dequeueReusableCellWithIdentifier("Cellule", forIndexPath: indexPath) as! CelluleView
return cellule
}
}
Upvotes: 0
Views: 229
Reputation: 7906
One solution would be to enable selection in the table view.
Set the selection style to none
override func awakeFromNib() {
super.awakeFromNib()
selectionStyle = UITableViewCellSelectionStyle.None
}
Create an outlet for your textfield
@IBOutlet weak var textField: UITextField!
and connect it with the nib
Then in the cell respond to the selection and set the text field to become first responder
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
textField.becomeFirstResponder()
}
Upvotes: 2