简体   繁体   English

UITableView中的UITextField与DatePicker的InputView

[英]UITextField in UITableView with InputView of DatePicker

I am attempting the following: 我正在尝试以下操作:

Within each cell I have date-from and date-to UITextField . 在每个单元格中,我都有date-from和date-to UITextField When the user presses either TextField an InputView of the DatePicker appears with a Done button to complete. 当用户按下任一TextField时,将显示DatePicker的InputView并带有“完成”按钮以完成操作。

My challenge is I only want the user to be able to select either of the TextFields inside the selected cell and no other. 我的挑战是我只希望用户能够选择所选单元格内的任何TextField,而不能选择其他任何文本字段。 The user must press Done on InputView when they have finished, so the app can validate and reload TableView if necessary. 用户必须在完成输入后在InputView上按Done,以便该应用可以验证并在必要时重新加载TableView。

I have code that manages normal cell selection well when the 'keyboard' is shown, but cannot manage the UITextField behavior as I would like. 当显示“键盘”时,我的代码可以很好地管理正常的单元格选择,但无法按我的UITextField管理UITextField行为。

I share some code in the hope someone can help me: 我分享一些代码,希望有人能帮助我:

- (void)keyboardWillShow:(NSNotification*)aNotification {
    self.TableViewDiary.allowsSelection = NO;
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
    self.TableViewDiary.allowsSelection = YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityDiaryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ActivityDiaryCellId"];

    cell.activity = [self.activitycollection objectAtIndex:indexPath.row];
    cell.LabelName.text = cell.activity.name;
    cell.TextFieldStartDt.text = [self FormatPrettyTime :cell.activity.startdt];
    cell.TextFieldEndDt.text = [self FormatPrettyTime :cell.activity.enddt];

    cell.datePickerStart.date = cell.activity.startdt;
    cell.datePickerEnd.date = cell.activity.enddt;
    cell.tag = indexPath.row;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

ActivityDiaryCell ActivityDiaryCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code

    self.datePickerStart = [[UIDatePicker alloc] init];
    self.datePickerStart.datePickerMode = UIDatePickerModeDateAndTime;
    //self.datePickerStart.date = self.activity.startdt;
    [self.datePickerStart addTarget:self action:@selector(datePickerStartValueChanged:) forControlEvents:UIControlEventValueChanged]; // method to respond to changes in the picker value

    self.datePickerEnd = [[UIDatePicker alloc] init];
    self.datePickerEnd.datePickerMode = UIDatePickerModeDateAndTime;
    //self.datePickerEnd.date = self.activity.enddt;
    [self.datePickerEnd addTarget:self action:@selector(datePickerEndValueChanged:) forControlEvents:UIControlEventValueChanged]; // method to respond to changes in the picker value

    self.datePickerToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 44)];
    [self.datePickerToolbar setTintColor:[UIColor grayColor]];
    UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dismissPicker:)];
    UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [self.datePickerToolbar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];

    self.TextFieldStartDt.delegate = self;
    self.TextFieldStartDt.inputView = self.datePickerStart;
    self.TextFieldStartDt.inputAccessoryView = self.datePickerToolbar;

    self.TextFieldEndDt.delegate = self;
    self.TextFieldEndDt.inputView = self.datePickerEnd;
    self.TextFieldEndDt.inputAccessoryView = self.datePickerToolbar;
}

Keyboard is showing because a user has tapped a text field, not because they selected a cell. 显示键盘是因为用户点击了文本字段,而不是因为他们选择了一个单元格。 That's why allowsSelection doesn't affect it. 这就是为什么allowsSelection不影响它的原因。

Move the UITextFieldDelegate handling to your UITableViewController . UITextFieldDelegate处理移动到UITableViewController Then you can easily decide whether to show a date picker or not: 然后,您可以轻松决定是否显示日期选择器:

  • if date picker is hidden, show it and remember a cell which textfield is on 如果日期选择器是隐藏的,则显示它并记住文本字段所在的单元格
  • if date picker is already visible, check whether text field belongs to the same cell and either update date picker or do nothing (by overriding textFieldShouldBeginEditing(_:) and returning false ) 如果日期选择器已经可见,请检查文本字段是否属于同一单元格,并更新日期选择器还是不执行任何操作(通过重写textFieldShouldBeginEditing(_:)并返回false

I moved the UITextFieldDelegate up to the TableViewController and included following code there: 我将UITextFieldDelegate移至TableViewController并在其中包括以下代码:

-(bool) textFieldShouldBeginEditing:(UITextField *)textField {
if (self.TableViewDiary.allowsSelection) {
    return true;
} else {
    CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:self.TableViewDiary];
    NSIndexPath *indexPath = [self.TableViewDiary indexPathForRowAtPoint:buttonPosition];
    ActivityDiaryCell *cell = [self.TableViewDiary cellForRowAtIndexPath:indexPath];
    if ([cell.TextFieldStartDt isFirstResponder] || [cell.TextFieldEndDt isFirstResponder]) {
        return true;
    }
}
return false;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:self.TableViewDiary];
NSIndexPath *indexPath = [self.TableViewDiary indexPathForRowAtPoint:buttonPosition];
ActivityDiaryCell *cell = [self.TableViewDiary cellForRowAtIndexPath:indexPath];
[cell setSelected:YES animated:NO];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:self.TableViewDiary];
NSIndexPath *indexPath = [self.TableViewDiary indexPathForRowAtPoint:buttonPosition];
ActivityDiaryCell *cell = [self.TableViewDiary cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM