0

I have a tableViewCell in a TableView that gets big if you click on it and if you click it again it goes back to its original size.

What I'd like it to do is, display a datePicker when its big but every time I try to simply add a datePicker in my code it is at the bottom of the tableView and not inside the big cell.

This is my code for adding the datePicker when the cell gets big.

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

    if (indexPath.row == 0) {

        return 150.0;
    }
    else if ([self cellIsSelected:indexPath] && indexPath.row == 1 ){

                [_dateLabel removeFromSuperview];  //just the label of the cell

                // Add the picker
                UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 350, 200)];
                pickerView.datePickerMode = UIDatePickerModeDate;

                [_cell addSubview:pickerView];

               return kCellHeight * 4.0;

            }
    else if (![self cellIsSelected:indexPath]) {

                [_myPicker removeFromSuperview];
            }

    return kCellHeight;
}

How do I add a UIDatePicker to a TableViewCell?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335

1 Answers1

0

Please take a look at this answer on SO, as it may help you further.

Just remember to set the delegates:

@interface YourTableViewViewController : UITableViewController <UIPickerViewDataSource, UIPickerViewDelegate>

And make sure you return values from these methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return <NUM_OF_SECTIONS>;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return <NUM_OF_ROWS_IN_SECTION>;    
}

For the rest, it is self-explanatory there.

Community
  • 1
  • 1
Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • Comment not relevant as it talks about a UIPickerView and not a UIDatePicker – aleene Sep 28 '16 at 15:11
  • Point is that the UIDatePicker dos not have a delegate (nor datasource, obviously). Thus any change in the picker must be handled differently, if you want to pick up the result in the tableviewController. – aleene Oct 01 '16 at 17:09