Managing Delegate of UITextFields in Custom UITableViewCell

So I have looked around a quite a bit, and nothing on here seems to explain exactly the correct way of doing this. I have 7 UITextFields within a custom UITableViewCell.

My question is this: What is the correct way of managing the delegate of these UITextFields?

Since the custom cells are technically part of the "model" portion of the project, I would rather have the controller that controls the UITableView also control the text fields in the table's cells, but I can not figure out how to set the delegate for the text fields (which are created in the subclass of UITableViewCell) to this view controller.

Would it be bad practice to just make the subclass of UITableViewCell conform to UITextField delegate and manage all of that stuff in there? If so, how else should I go about doing this?

Thanks, any help would be appreciated.


You shouldn't have a problem setting the delegate of the cell's text field to be your view controller.

This is what you need to do:

1) The view controller needs to implement the UITextFieldDelegate protocol

2) Declare a property for the text field in your custom cell

@property (nonatomic, retain) IBOutlet UITextField *textField;

3) Then set the view controller as the text field's delegate in the method cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *cellIdentifier = @"Cell";

    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) 
        // use this if you created your cell with IB
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil] objectAtIndex:0];   

        // otherwise use this  
        cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

        // now set the view controller as the text field delegate  
        cell.textField.delegate = self;

    // configure cell...

    return cell;

In my opinion the cell should manage the keyboard since it is the one that is holding the UITextField. You can set your cell as the UITextField delegate. In my own application, I have done this and then made my cell have it's own delegate. Any of the methods of UITextField or any new methods that should be handled by the controller can be passed along to the controller through the cells delegate.

In this way the cell can still be generic without knowing anything about what the application is actually doing.

My suggestion would be to "tag" (i.e. set the tag) of each textField with a value that encodes the section, row, and one-of-7 text views in the table, then make the UIViewController the delegate.

So you need to bound the size of these - say you will never have more than 100 rows. So you encode this as:

.tag = 1000*section + 100*row +

When you get a message you can have a method/function take the tag and decode it into section, row, tag, and do what you need to have done.

To declare your TableViewController as the delegate include <UITextFieldDelegate> at the end of your @interface in the TableViewController's .h file.

@interface MyTableViewController : UITableViewController <UITextFieldDelegate>

Then, connect the text fields by ctrl-dragging each field under the @interface. Each UITextField is connected to its respective property by an IBOutlet. Finally, in the .m file include the following function to show the delegate which field you want to return....

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [aTextField resignFirstResponder];
    return YES;

Swift version (Based on Eyal's answer)

class MyViewController: UIViewController, ... , UITextFieldDelegate {

    @IBOutlet var activeTextField: UITextField!  //doesn't need to connect to the outlet of textfield in storyboard


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


    var cellTextField = self.view.viewWithTag(101) as? UITextField
    cellTextField!.delegate = self;

Need Your Help

What single characteristic is most important for a good routine?

function methods procedures routines

Routines, procedures, methods - whatever you call them, they are important building blocks for us developers. What single characteristic would you rate as the most important one?