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

标签 ios objective-c uitableview uitextfield uitextfielddelegate

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

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

- (IBAction)plusButtonPressed:(id)sender;

enter image description here

我将文本字段的所有值存储在 View Controller 内的数组中。问题是,它不会在第一次加载时更新,但如果我添加或删除行,它确实可以工作。

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

下面是表格 View 单元格中的委托(delegate)方法:

#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 的委托(delegate)是重点。

确保它在开始时设置正确。

关于ios - 自定义 UITableViewCell 中的 UITextField 委托(delegate)不会在第一次加载时被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57482197/

相关文章:

ios - 使用我的自定义初始值来初始化我的表格单元格(继承 UITableViewCell)

ios - UIBezierPath 为每个部分放置一种颜色,导致最后一个颜色

iphone - 对大整数进行舍入 - Objective-C

iOS 在方向改变时触发滚动事件

ios - 静态菜单场景中的 SpriteKit 内存泄漏

ios - 调用 [tableView reloadData] 时 TableView 未重新加载

ios - 如何清除或不设置 kLinearPCMFormatFlagIsNonInterleaved

ios - UIAppearance 的 "when not contained in"

ios - 从 viewdidload 将行插入到 TableView 中

ios - 单元格中的标签高度不会动态改变高度