waseefakhtar
waseefakhtar

Reputation: 1423

implementing UITextFieldDelegate

I want to use https://github.com/intuit/AnimatedFormFieldTableViewCell in my project but I can’t get my head around the steps for setting it up. So far, I’ve dragged the files from the folder and followed the steps as:

In order to use AnimatedFormFieldTableViewCell, all you have to do is:

1) Register the AnimatedFormFieldTableViewCell nib on the ViewController on which you are implementing your UITableView for your reuse identifier.

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.registerCellNib(AnimatedFormFieldTableViewCell)

    tableView.reloadData()
}

2) Dequeue your cell on CellForRowAtIndexPath as a AnimatedFormFieldTableViewCell.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! AnimatedFormFieldTableViewCell

    return cell
}

3) Change the placeholder's label text by calling setLabelText on the cell itself.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! AnimatedFormFieldTableViewCell

    cell.setLabelText("Enter title")

    return cell
}

Notes:

1) You can still implement UITextFieldDelegate the same way you would implement on a regular UITextField, you just need to define the AnimatedFormFieldTableViewCell's delegate (there is no need to directly define the delegate for the embedded UITextField).

2) In order to access the embedded UITextField you can simply call the cell's cellTextField property.

I don’t get these last two steps.

If I run my app, I get the unexpectedly found nil on self.cellTextfield.delegate = self in the AnimatedFormFieldTableViewCell class.

What am I missing?

Upvotes: 0

Views: 179

Answers (3)

Ayoub Nouri
Ayoub Nouri

Reputation: 201

Hi waseefakhtar your code does not miss anything. just the only problem is you registered the nib with the wrong way that's why you got the unexpectedly found nil error on self.cellTextfield.delegate = self.

try this code:

  let myNib = UINib.init(nibName: "AnimatedFormFieldTableViewCell", bundle: nil)
  self.tableView.registerNib(myNib, forCellReuseIdentifier: "cell")

PS: just be aware that registerNib method syntax can differ depending on the swift version

Upvotes: 2

aircraft
aircraft

Reputation: 26886

My code is here, I don't know where you clerical error, but you can check my code to find out:

import UIKit

class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // register a nib like below 
        let nib:UINib = UINib.init(nibName: "AnimatedFormFieldTableViewCell", bundle: nil)

        self.tableView.register(nib, forCellReuseIdentifier: "AnimatedFormFieldTableViewCell")

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return 88.0
   }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "AnimatedFormFieldTableViewCell", for: indexPath as IndexPath) as! AnimatedFormFieldTableViewCell

        cell.setLabelText("Enter title")

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)
    }

}

The result:

enter image description here

So, just check the steps, where you go wrong.

Upvotes: 1

Michael Fourre
Michael Fourre

Reputation: 3015

The documentation notes this:

There is no need to directly define the delegate for the embedded UITextField

The instruction is to do the following:

You just need to define the AnimatedFormFieldTableViewCell's delegate

You'll want to set the AnimatedFormFieldTableViewCell's delegate rather than the cellTextField's delegate, so:

self.delegate = yourUITextViewDelegate

rather than self.cellTextField.delegate = yourUITextViewDelegate (this is wrong)

This is what the documentation describes

Edit: OP clarified that the delegate assignment code is not in their class; it's in the pod's class. I'm considering deleting this answer as I don't think it is solving the problem.

Upvotes: 0

Related Questions