首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内联UIPicker实现

内联UIPicker实现
EN

Stack Overflow用户
提问于 2015-02-12 18:48:49
回答 1查看 1.2K关注 0票数 1

我试图在一个表视图单元格内实现一个内联UIPicker,类似于thisthis。我相信我的实现非常接近,但目前,当我选择适当的单元格时,没有显示任何选择器。对于我做错了什么,谁能指出正确的方向吗?谢谢!

下面是我确定每一节中出现哪些行的地方:

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case NotificationsSection:
            return [self tableView:tableView cellForAreaOneRowAtIndexPath:indexPath];
            break;
        case RedZoneSection:
            return [self tableView:tableView cellForAreaTwoRowAtIndexPath:indexPath];
            break;
        case TimeOfDaySection:
            return [self tableView:tableView cellForAreaThreeRowAtIndexPath:indexPath];
            break;
        default:
            return nil;
            break;
    }
}

下面是我检查每个部分的行数的地方。我怀疑我的问题可能在这里,但我不完全确定。

代码语言:javascript
复制
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case AreaOneSection:
            return AreaOneRows;
            break;
        case AreaTwoSection:
            return TotalAreaTwoRows;
            break;
        case AreaThreeSection:
            return TotalAreaThreeRows;
            break;
        default:
            return 0;
            break;
    }
}

下面是我返回每一行的高度的地方:

代码语言:javascript
复制
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat rowHeight = self.tableView.rowHeight;
    //    if (indexPath.section == TimeOfDaySection && indexPath.row == HourTimeZoneRow  && self.timePickerIsShowing == NO){
    return rowHeight;
}

最后,下面是我检查用户是否选择了要插入下面的UIPicker单元格的索引路径的地方。如果他们这样做了,那么我就调用一个方法来显示采摘者。

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section == SectionThree && indexPath.row == RowOne  && self.timePickerIsShowing == NO){

        [tableView beginUpdates];
        [self showTimePicker];
        [tableView endUpdates];
    } else{
        [self hideTimePicker];
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

最后,下面是我展示和隐藏UIPicker的地方。

代码语言:javascript
复制
- (void)showTimePicker
{
    self.timePickerIsShowing = YES;
    self.timePicker.hidden = NO;
    //build the index path to where the picker should be inserted here
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:HourTimeZoneRow + 1 inSection:TimeOfDaySection];

    static NSString *CellIdentifier = @"TimePickerCell";
    UITableViewCell *cell = (UITableViewCell*)[self.tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    _timePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 160)];
    [cell.contentView addSubview:self.timePicker];

    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView reloadData];
    self.timePicker.alpha = 0.0f;
    [UIView animateWithDuration:0.25 animations:^{
        self.timePicker.alpha = 1.0f;
    }];
}

- (void)hideTimePicker {
    self.timePickerIsShowing = NO;
    self.timePicker.hidden = YES;
    [self.tableView reloadData];
    [UIView animateWithDuration:0.25
                     animations:^{
                         self.timePicker.alpha = 0.0f;
                     }
                     completion:^(BOOL finished){
                         self.timePicker.hidden = YES;
                     }];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-12 19:08:19

在您的showPicker函数中,您似乎什么也不做?你创建一个细胞,用它做一些事情,当这个功能结束时它就会死掉。在我能看到的任何地方都没有添加这个单元格?

您需要在cellForRowAtIndexPath中添加选择器,因为您知道的索引路径需要一个选择器。

我所做的几乎不需要编码。我在接口生成器中创建了一个原型单元格,其中包含一个选择器视图。在我的例子中,我还在选择器的上方添加了一个工具栏,我可以在其中放置按钮以允许取消和完成。添加适当的属性以传递当前值,供选择器最初显示。添加一个委托,用于通知创建者(您的tableView)选择器值的更改。您可以等待它完成挑选,或者通过每次值更改时重新加载正在编辑的单元格来更新活动单元格的编辑值。我更希望选择器可以选择一个值,而您可以提交它或取消它。

当单元格需要编辑时,我更新数据模型以插入编辑条目,然后调用[tableView reload]。在我的示例中,选择一个单元格开始编辑,单击cancel/done结束编辑。

此时的表视图将开始请求单元格。这一次,其中一个将是您的新选择单元格,它将用于编辑下面的单元格。创建它时,将要编辑的数据的数据模型引用传递给它。

因此,您可以通过简单地添加一个新的原型单元格类型并在需要时在cellForRowAtIndexPath中创建它来实现所有这些。

您可以选择如何移除拾取器。在我的例子中,我有Cancel/Done按钮,它只是从数据模型中删除条目,然后再次重新加载表,从而导致它永远不会被创建。您还可以将模式设置为单击要添加的单元格,然后再次单击以删除。同样,您只需更新数据模型并重新加载。看看时间选择器如何在Calander应用程序上进行新的约会。

你可能会认为这是大量的重新装载。然而,它只影响屏幕上的内容,我发现它比一直试图找出受影响的细胞容易得多。它也很光滑。当它开始工作时,您可以始终优化代码。

使用单元格中的约束确保它具有所需的布局。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28485399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档