内联UIPicker实现

我试图在表视图单元内实现一个内联的UIPicker,类似于这个和这个 SO问题。 我相信我在实施中很接近,但是现在,当我select合适的单元格时,不会显示select器。 任何人都可以指出我在正确的方向,我做错了什么? 谢谢!

以下是我确定每个部分中发生的行的位置:

- (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; } } 

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

 - (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; } } 

以下是我返回每一行的高度:

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

最后,下面是我检查用户是否select了我想在下面插入UIPicker单元格的索引path。 如果他们这样做,那么我打电话给方法来显示select器。

 - (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的地方。

 - (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; }]; } 

在你的showPicker函数中,你似乎没有做任何事情? 你创build一个单元格,用它做事情,然后当这个函数结束时它会死亡。 单元格不是从我所能看到的任何地方添加的?

您需要在cellForRowAtIndexPath中添加select器,因为您知道的索引path需要一个select器。

我所做的只需要很less的编码。 我在界面生成器中创build一个包含选取器视图的原型单元格。 在我的情况下,我还添加了一个工具栏上方的select器,我可以把button,以允许取消和完成。 添加合适的属性以传递选取器的当前值以最初显示。 添加一个委托来通知创build者(你的tableView)select器值的变化。 您可以等待它完成拾取,或者使用它通过每次更改值时重新加载正在编辑的单元格更新其编辑值的单元格。 我更喜欢select器可以select一个值,你可以提交或取消它。

当一个单元格需要编辑时,我更新我的数据模型插入​​一个编辑条目,然后调用[tableView reload] 。 在我的情况下,select一个单元格开始编辑,点击取消/完成结束编辑。

此时表格视图将开始询问单元格。 这一次,其中一个将是你的新的select器单元格,这将是编辑下面的单元格。 当您创build它时,您将它传递给它要编辑的数据的数据模型引用。

所以你可以通过简单地添加一个新的原型单元格types并在需要时在cellForRowAtIndexPath创build它来实现所有这些。

您可以select如何删除选取器。 在我的情况下,我有取消/完成button,只是从数据模型中删除条目,并重新加载表,导致它永远不会被创build。 你也可以使模式点击一个单元格添加并再次点击删除。 再次,您只需更新数据模型并重新加载。 看看时间select器如何在新的约会的carander应用程序上工作。

您可能会认为这是很多重新加载。 然而,它只影响到屏幕上的内容,我发现它总是比试图弄清楚受影响的单元格更容易。 它也非常顺利。 工作时,您可以随时优化代码。

在单元格中使用约束来确保它具有您想要的布局。