自定义 UITableViewCell 内的文本字段委托(delegate)不会被调用。此外,单元格末尾有一个按钮,可以动态添加或删除行。
@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet UIButton *plusButton;
- (IBAction)plusButtonPressed:(id)sender;
我将文本字段的所有值存储在 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/