【问题标题】:UITextField delegate inside custom UITableViewCell does not get called for the first load自定义 UITableViewCell 中的 UITextField 委托在第一次加载时不会被调用
【发布时间】:2019-12-20 06:33:33
【问题描述】:

自定义 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];
}

下面是表格视图单元格的委托方法:

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

【问题讨论】:

  • “标题本身是不言自明的,我希望”它不是。你没有解释你做什么,以什么顺序,以及你期望调用什么委托方法。
  • 我编辑了我的问题,
  • @SetoElkahfi - 你在哪里设置 textField 上的委托?
  • self.delegate 我猜这是UITableViewCell 的委托,你必须在ViewController 中设置这个委托。最好的地方是cellForRow方法。

标签: ios objective-c uitableview uitextfield uitextfielddelegate


【解决方案1】:

我认为 UITextField 的委托是重点。

确保一开始就正确设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多