自定义 UITableViewCell 中的 UITextField 委托不会在第一次加载时被调用

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

自定义 UITableViewCell 中的文本字段委托未被调用。此外,单元格末尾有一个按钮,可以动态添加或删除行。

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

- (IBAction)plusButtonPressed:(id)sender;

我将文本字段的所有值存储在视图控制器内的数组中。问题是,它不会在第一次加载时得到更新,但如果我添加或删除行,它确实有效。

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

下面是来自 table 视图单元格的委托方法:

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

我认为 UITextField 的委托是重点。

确保在开始时设置正确。