Integrating UIDatePicker into a UITableView Using Apple's DateCell Example

UIDatePicker and a UITableView

=====================================

In this article, we will explore how to integrate a UIDatePicker into a UITableView using Apple’s DateCell example as a starting point. We will also delve into the implementation of the pickerView:didSelectRow:inComponent: method to update the cell label with the selected date.

Background


The DateCell example provided by Apple is a great resource for creating a table view with a date cell that can be used to select dates. In this article, we will build upon this example to add a UIDatePicker to our table view.

Setting Up the Table View Controller


To start, let’s create a new table view controller and configure it as follows:

  • Set up your table view with a single section and one row.
  • Configure the cell type to be a date cell using Apple’s DateCell example.
// Create a new table view controller

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Set up the table view
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:tableView];
}

// Configure the cell type to be a date cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell
    cell.textLabel.text = @"Date";
    
    return cell;
}

Adding the UIDatePicker


Next, let’s add a UIDatePicker to our table view controller. We will create an outlet for the picker and configure it as follows:

  • Create an outlet for the UIDatePicker.
  • Set the picker’s center point outside of the screen boundaries.
  • Hide the picker by default.
// Create an outlet for the UIDatePicker

@property (nonatomic, strong) UIPickerView *typePicker;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Create an outlet for the UIDatePicker
    self.typePicker = [[UIPickerView alloc] init];
    [self.view addSubview:typePicker];
}

// Configure the picker's center point outside of the screen boundaries

- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the picker's center point outside of the screen boundaries
    [typePicker setCenter:CGPointMake(150, 500)];
    
    // Hide the picker by default
    [typePicker setHidden:YES];
}

// Animate the picker to slide up when it becomes visible

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if ([self.typePickerHidden]) {
        [UIView beginAnimations:@"slideIn" context:nil];
        [self.typePicker setCenter:CGPointMake(150, 250)];
        [UIView commitAnimations];
    }
}

// Flag to track whether the picker is visible

@property (nonatomic, assign) BOOL typePickerHidden;

Implementing the didSelectRow:inComponent: Method


Next, let’s implement the pickerView:didSelectRow:inComponent: method to update the cell label with the selected date.

// Implement the pickerView:didSelectRow:inComponent: method

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // Get the current table view cell
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    
    // Update the cell label with the selected date
    cell.textLabel.text = [NSString stringWithFormat:@"%ld-%02d-%02d", (long)row, row % 12 + 1, row / 12 + 1];
}

Implementing the Delegate Methods


Finally, let’s implement the delegate methods required by the UIDatePicker and the table view.

// Implement the UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // ... (existing code)
}

// Implement the UIPickerViewDataSource methods

- (NSInteger)pickerViewNumberOfRowsInComponent:(NSInteger)component {
    return 10;
}

- (NSString *)pickerViewTitleForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    switch (interfaceOrientation) {
        case UIInterfaceOrientationPortrait:
            return @"Date";
        default:
            break;
    }
    
    return @"";
}

Conclusion


In this article, we have successfully integrated a UIDatePicker into a UITableView using Apple’s DateCell example as a starting point. We have also implemented the necessary delegate methods to update the cell label with the selected date.

By following these steps and implementing the required delegate methods, you should be able to create a table view with a date cell that can be used to select dates using a UIDatePicker.


Last modified on 2025-05-08