0

簡単なメモ ブロック アプリケーションを作成しようとしています。AQGridView を使用して、ノート ブロックをグリッドに表示しています。すべてのメモ ブロックには、AQGridViewCell のカスタム サブクラスに編集可能な UITextField プロパティがあります (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;

}

cellForItemAtIndex に textField のデリゲートを設定します (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);

選択した textField が同じ行にない場合でも、常に同じ値 (私の場合は 95) を返します。

これを解決する方法、または他の解決策についてのヒントは素晴らしいでしょう!

4

1 に答える 1

0

セルの textField にタグを設定し、そのタグ/インデックスからセルを取得することで解決したと思います。次に、セル内の textField ではなく、セルの位置に基づいてキーボードが表示される位置を計算します。

于 2011-07-30T10:42:47.320 に答える