使用AQGridView滚动到所选的textField(类似于tableView)

时间:2011-07-29 19:00:50

标签: objective-c ios uitableview aqgridview

我正在尝试创建一个简单的音符块应用程序。我正在使用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);

它总是返回相同的值,在我的情况下为95,即使所选的textField不在同一行上。

如何解决这个或其他解决方案的提示会很棒!

1 个答案:

答案 0 :(得分:0)

我想我通过在单元格的textField中设置标签,然后从该标签/索引中获取单元格来解决它。然后根据单元格的位置而不是单元格内的textField计算键盘出现时的位置。