0

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
}
}
DRV
  • 3
  • 3

1 Answers1

2

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()
}
Moriya
  • 6,855
  • 3
  • 27
  • 48
  • it works! but then it gets centered around the textfield and I can't scroll down to other cells unless I select them. I also added a button next to the textfield but I can't "click" it... – DRV Oct 22 '15 at 00:53
  • This makes me wonder how your views are set up... I've never experienced any issues like this. – Moriya Oct 22 '15 at 01:20
  • When I run in to similar issues I usually use the view hierarchy tool shown in the second answer in this post http://stackoverflow.com/questions/5150186/how-do-i-inspect-the-view-hierarchy-in-ios It's a great go to to check if there are any views blocking the touch or similar. – Moriya Oct 22 '15 at 01:25