簡體   English   中英

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

下面是表格視圖單元格中的委托方法:

#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的委托很重要。

確保開始時設置正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM