Seto
Seto

Reputation: 1666

UITextField delegate inside custom UITableViewCell does not get called for the first load

The textfield delegate inside the custom UITableViewCell not get called. In addition, there's a button on the end of the cell that will add or remove row dynamically.

@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet UIButton *plusButton;

- (IBAction)plusButtonPressed:(id)sender;

enter image description here

I store all the values of the text fields in an array inside the view controller. Problem is, it's not get updated at the first load, but it does work if I add or remove row.

#pragma mark - TextFieldTableViewCellDelegate

- (void)addButtonPressed {
    [self.secondaryRecipients addObject:@""];
    [self calculateOtherRecipientsTableViewHeight];
}

- (void)deleteButtonPressedAt:(NSIndexPath *)indexPath {
    [self.secondaryRecipients removeObjectAtIndex:indexPath.row];
    [self calculateOtherRecipientsTableViewHeight];
}

- (void)textFieldDidEndEditing:(UITextField *)textField at:(NSIndexPath *)indexPath {
    [self.secondaryRecipients replaceObjectAtIndex:indexPath.row withObject:textField.text];
}

- (void)textFieldDidChangeCharacter:(NSString *)string at:(NSIndexPath *)indexPath {
    [self.secondaryRecipients replaceObjectAtIndex:indexPath.row withObject:string];
}

- (void)calculateOtherRecipientsTableViewHeight {
    self.otherRecipientsTableViewHeight.constant = TABLEVIEW_CELL_DEFAULT_HEIGHT * [self.secondaryRecipients count];
    [self.otherRecipientsTableView reloadData];
    [self recalculateViewSize];
}

Below is the delegate methods from the table view cell:

#pragma mark UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldDidEndEditing:at:)]) {
        [self.delegate textFieldDidEndEditing:textField at:self.indexPath];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString * text = textField.text != nil ? textField.text : @"";
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldDidChangeCharacter:at:)]) {
        [self.delegate textFieldDidChangeCharacter:text at:self.indexPath];
    }
    return YES;
}

Upvotes: 0

Views: 298

Answers (1)

irons163
irons163

Reputation: 54

I think the delegate for UITextField is the point.

Make sure it set properly in the begin.

Upvotes: 1

Related Questions