6

A number of posts are dealing with the subject: How to make an inline UIPickerView. As I am lazy, can anyone point me to a code snippet. To be honest, I find the Apple DateCell sample pedantic - there has to be an a more elegant method.

Is the dateCell app a good place to start? or are there other better links. I would appreciate any advice.

If you read this and do not understand my requirements / goal, please see the two posts referenced above or simply download the Apple Sample (dev. account required).

Community
  • 1
  • 1
ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • Hi there David, I'm currently struggling with the same sort of problem. By any chance (now that you solved the problem), do you have any example code I would be able to take a look at? Thanks! – narner Feb 03 '15 at 20:25
  • 1
    Hi Narner, let me refresh my memory and get back to you. – ICL1901 Feb 03 '15 at 22:08
  • That'd be amazing David, I'd appreciate it! – narner Feb 04 '15 at 13:07
  • I ended up using an expand / contract tableviewcell structure, integrating this control by Tom Fewster: https://github.com/wannabegeek This control includes an inline date picker. Hope this helps. – ICL1901 Feb 04 '15 at 15:59
  • Thanks David, I tried running the project from that link, but unfortunately it was missing a file. I gave this ago, but ran into a problem (at this SO question: http://stackoverflow.com/questions/28353639/attempting-to-add-an-inline-uidatepicker-to-a-uitableviewcell). Would you mind taking a look at all? – narner Feb 05 '15 at 20:44
  • You have a new answer there, that looks reasonable. There is also this thread: http://stackoverflow.com/questions/18973573/ios-7-how-to-display-a-date-picker-in-place-in-a-table-view?lq=1 – ICL1901 Feb 05 '15 at 23:00

1 Answers1

9

I use another - maybe simpler - solution to solve this.

Image that we have two cells

  1. Date label cell
  2. Date picker cell

Most of the "magic" is within the table view delegate's tableView:heightForRowAtIndexPath: method:

- (CGFloat)tableView:(UITableView:)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat heightForRow = tableView.rowHeight;

    if ([self isDatePickerRowAtIndexPath:indexPath]) {
        heightForRow = (self.isDatePickerShown) ? heightOfDatePicker : 0.0;
    }

    return heightForRow;
}

So you simply "hide" the date picker by returning a height of 0.0.


In the tableView:didSelectRowAtIndexPath: method you do the toggling:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self isDateLabelRowAtIndexPath:indexPath])
    {
        self.datePickerIsShown = ! self.isDatePickerShown;
        [tableView beginUpdates];
        [tableView endUpdates];
    }
}

Calling the empty beginUpdates endUpdates block forces the table to call the tableView:heightForRowAtIndexPath: again (animated) and nicely fades in or out the date picker cell.


When the date picker cell is the last one in a section you might also want to update the date label cell's separatorInset to UIEdgeInsetsZero when the date picker is hidden and to the default value when it's shown.


EDIT:

For completeness: datePickerIsShown is a simple boolean:

@property(nonatomic, getter = isDatePickerShown) BOOL datePickerIsShown;

The methods isDateLabelRowAtIndexPath: and isDatePickerRowAtIndexPath: are just helper methods that compare a given indexPath to the known index path of the appropriate cell:

- (BOOL)isDatePickerRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ([self.datePickerIndexPath compare:indexPath] == NSOrderedSame);
}

EDIT 2:

There's one additional step missing: Make sure that you set the date picker cell's clipsToBounds property to YES, otherwise you'll get some view glitches.

Florian Mielke
  • 3,310
  • 27
  • 31