Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

uitableview - ios7 - UITableViewCell with UIPickerView and UIDatePicker built inline

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...