开发者

UITableView does not scroll correctly (or not at all)

I am trying to scroll in a UITableView, while a keyboard is showing. I resize the TableView, so that it is not covered by the keyboard. I overwrote the textFieldShouldReturn functio开发者_JAVA百科n to skip to the next UITextField in my UITableView, and also scroll to the next field. Here is the code I use:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    UITableViewCell *cell = (UITableViewCell *)[textField superview];
    NSIndexPath *indexPath = [self.prefTableView indexPathForCell:cell];
    NSString *nextRowKey = [preferences nextRowKeyAtIndexPath:indexPath];
    [textField resignFirstResponder];
    if (nextRowKey != nil) {
        tagBeingEdited = textField.tag + 1;
        NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1
                                                    inSection:indexPath.section];

        [self.prefTableView scrollToRowAtIndexPath:nextIndexPath
                                  atScrollPosition:UITableViewScrollPositionTop
                                          animated:YES];
        //[self.prefTableView selectRowAtIndexPath:nextIndexPath 
        //                                animated:YES
        //                          scrollPosition:UITableViewScrollPositionMiddle];
        if ([[self.prefTableView indexPathsForVisibleRows] containsObject:indexPath] == YES) {
            [[self.prefTableView viewWithTag:tagBeingEdited] becomeFirstResponder];
        }
    } else {
        isEditing = NO;
    }
    return NO;
}

However, my view never scrolls. Or if I manually have scrolled the view, it will go back to the top. Why?

Note: I only use one of scrollToRowAtIndexPath and selectRowAtIndexPath. I just put both of them there, since I first tried one and then the other.


@occulus is right that UITableViewController automatically handles resizing when the keyboard appears. If you want that behavior with a subclass of UIViewController, you need extra work.

The following link from Cocoa with love has an example of doing this.

http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜