-1

How can I add UIPickerView inside UITableViewCell?

Is there anything new with InlinePicker or just try with add as subview in cell?

Please answer my question & remove my confusion/doubt about inLinePicker

Juan Catalan
  • 2,299
  • 1
  • 17
  • 23
user2526811
  • 1,233
  • 4
  • 20
  • 54
  • 1
    possible duplicate of [iOS 7 - How to display a date picker in place in a table view?](http://stackoverflow.com/questions/18973573/ios-7-how-to-display-a-date-picker-in-place-in-a-table-view) – Ashish Kakkad Jun 01 '15 at 07:05

2 Answers2

1

Apple has given the sample code for DateCell

Here is some code snippet of it :

- (BOOL)hasPickerForIndexPath:(NSIndexPath *)indexPath
{
    BOOL hasDatePicker = NO;
    NSInteger targetedRow = indexPath.row;
    targetedRow++;
    UITableViewCell *checkDatePickerCell =
        [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:targetedRow inSection:0]];
    UIDatePicker *checkDatePicker = (UIDatePicker *)[checkDatePickerCell viewWithTag:kDatePickerTag];
    hasDatePicker = (checkDatePicker != nil);
    return hasDatePicker;
}

- (void)updateDatePicker
{
    if (self.datePickerIndexPath != nil)
    {
        UITableViewCell *associatedDatePickerCell = [self.tableView cellForRowAtIndexPath:self.datePickerIndexPath];
        UIDatePicker *targetedDatePicker = (UIDatePicker *)[associatedDatePickerCell viewWithTag:kDatePickerTag];
        if (targetedDatePicker != nil)
        {
            NSDictionary *itemData = self.dataArray[self.datePickerIndexPath.row - 1];

            [targetedDatePicker setDate:[itemData valueForKey:kDateKey] animated:NO];

        }

    }

}
- (BOOL)hasInlineDatePicker

{
    return (self.datePickerIndexPath != nil);
}

- (BOOL)indexPathHasPicker:(NSIndexPath *)indexPath

{
    return ([self hasInlineDatePicker] && self.datePickerIndexPath.row == indexPath.row);

}

- (BOOL)indexPathHasDate:(NSIndexPath *)indexPath

{
    BOOL hasDate = NO;
    if ((indexPath.row == kDateStartRow) ||

        (indexPath.row == kDateEndRow || ([self hasInlineDatePicker] && (indexPath.row == kDateEndRow + 1))))

    {
        hasDate = YES;

    }
    return hasDate;
}
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
0

From This link

You will have to allocate UIPickerView in cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if(indexPath.row == pickerRow){
UIPickerView *pickerView = [[UIPickerView alloc]init];
cell = ... // alloc and initialize a cell
cell addSubview:pickerView];
}
else{ // your other cells }   

return cell;
}
iAnurag
  • 9,286
  • 3
  • 31
  • 48