I'm attempting to implement an inline UIPicker inside a table-view cell, similar to both this and this SO question. I believe I'm close in my implementation, but at the moment, no picker is displayed when I select the appropriate cell. Can anyone point me in the right direction in regards to what I'm doing wrong? Thank you!
Below is where I determine what rows occur in each section:
- (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;
}
}
Below is where I check the number of rows in each section. I suspect my problem may lie here, but I am not completely sure.
- (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;
}
}
Below is where I return the height for each row:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat rowHeight = self.tableView.rowHeight;
// if (indexPath.section == TimeOfDaySection && indexPath.row == HourTimeZoneRow && self.timePickerIsShowing == NO){
return rowHeight;
}
Finally, below is where I check if the user selected the index path that I want to insert the UIPicker cell below. If they did, then I call a method to show the the picker.
- (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];
}
}
Finally, below is where I show and hide the 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;
}];
}