【问题标题】:iOS Show UIDatePicker between UITableViewCellsiOS 在 UITableViewCells 之间显示 UIDatePicker
【发布时间】:2013-10-05 01:58:39
【问题描述】:

在 iOS 7 中,鼓励开发人员在需要输入时在表格单元格之间显示日期选择器,然后在完成时隐藏它们。怎样才能达到这个效果?

【问题讨论】:

标签: objective-c ios7 uidatepicker


【解决方案1】:

Vasilica Costescu 在这里有一个很棒的教程: http://masteringios.com/blog/2013/10/31/ios-7-in-line-uidatepicker/

对于静态表: http://masteringios.com/blog/2013/11/18/ios-7-in-line-uidatepicker-part-2/

此处的示例代码:https://github.com/costescv/InlineDatePicker

关键位是隐藏/显示方法:

 - (void)showDatePickerCell {
    self.datePickerIsShowing = YES;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

    self.datePicker.hidden = NO;
    self.datePicker.alpha = 0.0f;

    [UIView animateWithDuration:0.25 animations:^{
        self.datePicker.alpha = 1.0f;
    }];
}

- (void)hideDatePickerCell {
    self.datePickerIsShowing = NO;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

    [UIView animateWithDuration:0.25
                     animations:^{
                         self.datePicker.alpha = 0.0f;
                     }
                     completion:^(BOOL finished){
                         self.datePicker.hidden = YES;
                     }];
}

并且这个 UITableViewDelegate 方法将通过将其高度设置为 0 来“隐藏”该行:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0 && indexPath.row == 4 && self.datePickerIsShowing == NO){
        // hide date picker row
        return 0.0f;
    }
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

您可以通过按钮调用隐藏/显示方法,或者仅通过选择表格中的行来调用。 (注意:如果其他行有文本字段,则可能需要在 textFieldDidBeginEditing 委托方法中隐藏 datePicker)。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 4) {
        if (self.datePickerIsShowing){
            [self hideDatePickerCell];
        }else {
            [self showDatePickerCell];
        }
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

编辑:在单个表中使用多个这些内联选择器视图时要小心。我注意到它们从情节提要中加载的速度很慢:iOS 7 slow to open UITableViewController with UIPickerView

【讨论】:

  • @Antony F 您是否有使用此代码和多个部分的经验?!
  • @chrizstone - 是的。在我的示例中,第 0 部分的第 4 行只有一个日期选择器,但您可以在不同的行或部分中添加多个日期选择器。
  • 我试过了,但由于某种原因,有时会在错误的行中显示错误的选择器...
  • @chrizstone - 您是否正在从表格行中添加/删除选择器视图?不看代码很难猜出问题,所以你可能想发布另一个问题。当我在情节提要视图中有 3 个以上的选择器时(加载速度太慢),我必须动态创建选择器,但是一旦创建了每个选择器,我就会将它们留在行中。此外,由于一次激活多个选择器会使用户感到困惑,因此我最终添加了一个 NSIndexPath 属性来跟踪活动的选择器。
猜你喜欢
  • 1970-01-01
  • 2021-04-19
  • 2023-03-05
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多