UITableViewController с представлением выбора - PullRequest
4 голосов
/ 04 ноября 2010

У меня серьезная проблема, и я не могу решить ее самостоятельно. Я часами искал документацию, руководства по программированию, а также форумы разработчиков и переполнение стека.

Проблема в том, что я хочу отобразить представление выбора в UITableViewController. У меня есть экран с несколькими текстовыми полями, позволяющими выполнять поиск по названию / автору / ключевым словам ... и я также хотел бы указать минимальную и максимальную даты, используя UIDatePicker (или UIPickerView - чтобы указать "последние 5 дней") например).

Я хочу использовать UITableViewController, потому что это экономит мне много времени при изменении размера таблицы, когда клавиатура всплывает, когда пользователь нажимает текстовое поле. На самом деле мне никогда не удавалось воспроизвести эту анимацию с помощью UIViewController и прослушивания делегата текстовых полей. Это было почти идеально, но были некоторые видимые недостатки по сравнению с поведением таблицы при отображении с использованием UITableViewController.

Так что все хорошо, когда есть только текстовые поля. Но как насчет даты? Я хочу сделать это точно так же, как Contacts.app от Apple, когда я хочу добавить новый контакт и указать день рождения. В этом приложении отображается Date Picker, размер таблицы изменяется, переключение между полем электронной почты / телефона и днем ​​рождения прекрасно работает. Я мог бы полагать, что средство выбора даты в этом случае является клавиатурой, но не для ввода номера телефона / электронной почты, а для даты, потому что оно вставляется / выходит точно так же, как клавиатура, и мгновенно заменяется при открытии клавиатуры / средства выбора.

Как это удалось? Или где я могу найти самое простое решение для его воспроизведения. Я считаю, что это не может быть так сложно, потому что это очень распространенная ситуация.

С уважением Chris

Ответы [ 4 ]

7 голосов
/ 07 марта 2011

Все это бессмысленно.Мы должны иметь дело с inputView и inputAccessoryView, где inputView должен иметь средство выбора и inputAccessoryView панель инструментов.

1 голос
/ 04 ноября 2010

Вам нужно будет создать объект UIWindow, а затем добавить представление. Свойство windowLevel делает его выше, чем statusBar, который вы можете или не можете хотеть.

//statusWindow is a UIWindow ivar declared in the header
//pickerShowing is declared as a BOOL in header
//release and removeFromSuperview is done in the animation delegate methods

//ANIMATE IN

-(void)slideIn {
    CGRect pickerFrame = CGRectMake(0.0f, 0.0f, 320.0f, 200.0f); //guessing on height
    UIView *viewForPicker = [[UIView alloc] init];
    UIPickerView *aPicker = [[UIPickerView alloc] init]; //don't forget to set delegate and dataSource
    viewForPicker.frame = pickerFrame;
    statusWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0.0, 480.0, 320.0f, 200.0f)];//guessing on height, y value is off the screen (bottom)
    statusWindow.windowLevel = UIWindowLevelStatusBar;
    statusWindow.hidden = NO;
    statusWindow.backgroundColor = [UIColor clearColor];
    [statusWindow makeKeyAndVisible];
    [viewForPicker addSubview:aPicker];
    [statusWindow addSubview:viewForPicker];
    [viewForPicker release];
    [aPicker release];
    [UIView beginAnimations:@"slideUp" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    statusWindow.frame = CGRectMake(0.0f, 200.0f, 320.0f, 200.0f); //guessing on y and height values, change them to suit needs 
    [UIView commitAnimations];
    pickerShowing = YES;
}


//ANIMATE out:



-(void)slideOut {
    [UIView beginAnimations:@"slideDown" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    statusWindow.frame = CGRectMake(0.0f, 480.0f, 320.0f, 200.0f);
    [UIView commitAnimations];
    pickerShowing = NO;
}

-(void)animationFinished:(NSString *)name {
    if ([name isEqualToString:@"slideDown"]) {
        [statusWindow release];
    }
}
0 голосов
/ 06 марта 2011
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
    self.pickerView.date = [self.dateFormatter dateFromString:targetCell.detailTextLabel.text];

    // check if our date picker is already on screen
    if (self.pickerView.superview == nil)
    {
        [self.view.window addSubview: self.pickerView];

        // size up the picker view to our screen and compute the start/end frame origin for our slide up animation
        //
        // compute the start frame
        CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
        CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
        CGRect startRect = CGRectMake(0.0,
                                      screenRect.origin.y + screenRect.size.height,
                                      pickerSize.width, pickerSize.height);
        self.pickerView.frame = startRect;

        // compute the end frame
        CGRect pickerRect = CGRectMake(0.0,
                                       screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                       pickerSize.width,
                                       pickerSize.height);
        // start the slide up animation
        [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];

            // we need to perform some post operations after the animation is complete
            [UIView setAnimationDelegate:self];

            self.pickerView.frame = pickerRect;

            // shrink the table vertical size to make room for the date picker
            CGRect newFrame = self.tableView.frame;
            newFrame.size.height -= self.pickerView.frame.size.height;
            self.tableView.frame = newFrame;
        [UIView commitAnimations];

        // add the "Done" button to the nav bar
        self.navigationItem.rightBarButtonItem = self.doneButton;
    }
}

- (void)slideDownDidStop
{
    // the date picker has finished sliding downwards, so remove it
    [self.pickerView removeFromSuperview];
}

- (IBAction)dateAction:(id)sender
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.detailTextLabel.text = [self.dateFormatter stringFromDate:self.pickerView.date];
}

- (IBAction)doneAction:(id)sender
{
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    CGRect endFrame = self.pickerView.frame;
    endFrame.origin.y = screenRect.origin.y + screenRect.size.height;

    // start the slide down animation
    [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];

        // we need to perform some post operations after the animation is complete
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];

        self.pickerView.frame = endFrame;
    [UIView commitAnimations];

    // grow the table back again in vertical size to make room for the date picker
    CGRect newFrame = self.tableView.frame;
    newFrame.size.height += self.pickerView.frame.size.height;
    self.tableView.frame = newFrame;

    // remove the "Done" button in the nav bar
    self.navigationItem.rightBarButtonItem = nil;

    // deselect the current table row
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Вы можете загрузить полный рабочий пример приложения от Apple, демонстрируя только это.http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html

0 голосов
/ 04 ноября 2010

Если вы хотите сдвинуть / вывести окно выбора, вы можете использовать Core Animation.Простейший фрагмент кода:

// Slide picker view in
[UIView beginAnimations: @"SlideIn" context: nil];
myPickerView.frame = upFrame;
[UIView commitAnimations];

// ...

// Slide picker view out
[UIView beginAnimations: @"SlideOut" context: nil];
myPickerView.frame = downFrame;
[UIView commitAnimations];

upFrame и downFrame - это CGRect, который вы делаете с правильной позицией для вашего представления выбора на экране и вне экрана соответственно.

Надеюсь, это поможет.

...