Managing Delegate of UITextFields in Custom UITableViewCell

10,673

Solution 1

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;
}

Solution 2

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.

Share:
10,673
Ahab
Author by

Ahab

Updated on June 05, 2022

Comments

  • Ahab
    Ahab almost 2 years

    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.

  • Marcelo Gracietti
    Marcelo Gracietti over 7 years
    Thanks!! Worked for me
  • JeffB6688
    JeffB6688 about 7 years
    I agree. To accomplish this, just set the delegate in the initialization of your UITableViewCell implementation. For example, in the awakeFromNib call, just add self.YourTextField.delegate = self.