UIDatePicker Replaces Keyboard only on Last Uitableviewcell

时间:2018-07-21 20:49:24

标签: objective-c uitableview uitextfield uidatepicker custom-cell

I have a tableview where a user can add a new row that contains two UITextFields in them. The user can add a date to each textview (or not add one, leaving it empty) with a UIDatePicker. Once the process is completed and everything is saved, the user should be able to go to any row and update the dates in that row. The problem is that any row previous to the very last row created only shows the regular keyboard when it is time to edit. I can't seem to get the datepicker to show up except in the last saved row. Any thoughts on why this is happening?

Here is how I set up the datepicker:

-(void)showDatePicker{
    _datePicker = [[UIDatePicker alloc] init];

    UIToolbar *toolbar = [[UIToolbar alloc]init];
    [toolbar sizeToFit];

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)];

    doneButton.tintColor = [UIColor whiteColor];

    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    toolbar.translucent = YES;
    toolbar.barTintColor = [UIColor colorWithRed:8/255.0 green:46/255.0 blue:46/255.0 alpha:1];

    [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft,doneButton ,nil]];

    [_readDateCell.startDateTextfield setInputAccessoryView:toolbar];
    [_readDateCell.finishDateTextfield setInputAccessoryView:toolbar];

    [_datePicker setDatePickerMode:UIDatePickerModeDate];

    [_readDateCell.startDateTextfield setInputView:_datePicker];
    [_readDateCell.finishDateTextfield setInputView:_datePicker];
}

The function is called in two places - when the textfield begins editing and when a new row is being created:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    [self showDatePicker];
}

-(void)addNewRow {
    [self showDatePicker];
}

1 个答案:

答案 0 :(得分:1)

如果您要为文本字段设置输入视图,则无需在UITextField的didBeginEditing委托方法上调用showDatePicker。

然后,您需要在viewDidLoad方法中添加datePicker代码。在cellForRowAtIndexPath中,您需要设置inputAccessory和inputView