dave
dave

Reputation: 67

Display and hide DatePicker on UITableViewCell tap

I have a table view controller in which I need to display a date&time picker when a cell is tapped and hide it when the cell is tapped again. Basically the same effect that the iphone has when you choose your start and end date and time to create a new event in the calendar.

I'm guessing the display and hiding goes in the following method but I'm unsure of what goes inside:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

Some example code or link would be great. Thank you!!

Upvotes: 2

Views: 1367

Answers (2)

jherran
jherran

Reputation: 3367

Create your cell with whatever you want to show and the picker:

-----------------------------------
 cell visible part
-----------------------------------
 cell invisible part (with picker)
-----------------------------------

Define a property that let you know if you have to show the entire cell:

@property (nonatomic) BOOL shouldShowPicker;

Initialise this property (on viewDidLoad for example);

self.shouldShowPicker = NO;

A couple of methods to touch:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 4) { //where your picker row is
        self.shouldShowPicker = YES;
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 4 && self.shouldShowPicker) { //where your picker row is
         return CELL_VISIBLE_PLUS_INVISIBLE_PART;
    } else if(indexPath.row == 4 && !self.shouldShowPicker) {
         return return CELL_VISIBLE_PART;
    } else {
         return OTHER_CELLS_HEIGHT;
    }
}

Upvotes: 1

Rory McKinnel
Rory McKinnel

Reputation: 8014

You might find my answer here useful which describes what you need to do.

Inline UIPicker Implementation

Essentially you create a custom cell containing a date picker, with optional buttons. You then add this cell below the cell when you edit it and remove it when finished. All explained in the link.

Upvotes: 0

Related Questions