Выделите выделенное текстовое поле с помощью AQGridView (аналогично tableView). - PullRequest
0 голосов
/ 29 июля 2011

Я пытаюсь создать простое приложение для заметок.Я использую AQGridView для отображения блоков заметок в сетке.Каждый блок заметок имеет редактируемое свойство UITextField в пользовательском подклассе AQGridViewCell (аналогично TableViewCell).И у меня есть некоторые проблемы с обработкой клавиатуры.Я пытался следовать этому руководству, http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html. Я реализовал методы keyboardWillShow и keyboardWillBeHidden следующим образом:

- (void)keyboardWillShow:(NSNotification *)notification 
{

    // Animates the done button.
    [UIView beginAnimations:nil context:NULL];

    // Display a done button 
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done"    style:UIBarButtonItemStyleDone target:self action:@selector(hideKeyboard)];
    [[self navigationItem] setRightBarButtonItem:doneButton];

    NSDictionary* info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.gridView.contentInset = contentInsets;
    self.gridView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    NSLog(@"%f", activeField.frame.origin.y);
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y+kbSize.height);
        [self.gridView setContentOffset:scrollPoint animated:YES];
    }

}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification *)notification 
{
    // Remove the "done" button
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.gridView.contentInset = contentInsets;
    self.gridView.scrollIndicatorInsets = contentInsets;

}

Я установил делегат textField в cellForItemAtIndex (аналогично cellForRowAtIndexPath).

В настоящее время прокрутка в нужную позицию работает неправильно.Я думаю, что проблема связана с textField, потому что, когда я проверяю:

// activeField is a UITextField property that I set in textFieldDidBeginEditing, 
// and later set to nil in textFieldDidEndEditing.
NSLog(@"%f", activeField.frame.origin.y);

Он всегда возвращает одно и то же значение, в моем случае 95, даже если выбранный textField не находится в той же строке.

Советы о том, как решить это или другие решения, было бы здорово!

1 Ответ

0 голосов
/ 30 июля 2011

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

...