在didselectRowAtIndexPath之后,UITableView无法滚动到底部

时间:2013-12-12 12:42:05

标签: ipad uiscrollview scroll uitableview

我在iPAD中有一个UITableViewController,当我第一次加载tableview时,我可以滚动到底部,但是如果用didSelectRowAtIndexPath选择一个单元格,我就不能再滚动到底部,tableView继续弹回,它是一个非常奇怪的行为。我已经放了一些日志来获取所有的frame / bounds / contentSize / contentInset / contentOffset。 每次点击表高度都在不断增长,我无法理解如何解决这个问题。

 if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
            if ([self.navigationController.parentViewController isKindOfClass:[UISplitViewController class]]) {

            // iPad split view controller support
            UISplitViewController *split = (UISplitViewController *)self.navigationController.parentViewController;

            NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:split.viewControllers];
            NSArray *allControllersCopy = [allControllers copy];

            for(int i = 0;i < allControllersCopy.count;i++)
            {
                UIViewController *s = [allControllersCopy objectAtIndex:i];
                if ([s isKindOfClass:[UINavigationController class]]) {
                    NSLog(@"UINavigationController at index : %d",i);
                }
                NSLog(@"custom split : %@", s.title);

            }
            UINavigationController *nav = [split.viewControllers objectAtIndex:1];

            allControllers = [[NSMutableArray alloc] initWithArray:nav.viewControllers];

            for(int i = 0;i < allControllers.count;i++)
            {
                UIViewController *s = [allControllers objectAtIndex:i];
                if(![s isKindOfClass:[MVPTaskViewController class]])
                {
                    [nav popViewControllerAnimated:NO];

                }
            }
            self.nTaskViewController = [nav.viewControllers objectAtIndex:0];
            self.nTaskViewController.selectedTask = selectedTask;
            [self.nTaskViewController updateTask:self.nTaskViewController.selectedTask];

            NSIndexPath *cellAtIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
            [self.tableView scrollToRowAtIndexPath:cellAtIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

            NSLog(@"tableView frame height %f",self.tableView.frame.size.height);
            NSLog(@"tableView frame width %f",self.tableView.frame.size.width);

            NSLog(@"tableView contentSize height %f",self.tableView.contentSize.height);
            NSLog(@"tableView contentSize width %f",self.tableView.contentSize.width);

            NSLog(@"tableView content offset x %f - y %f",self.tableView.contentOffset.x, self.tableView.contentOffset.y);

        }

这是我得到的日志:

2013-12-12 12:33:47.854 moveplanner[7604:70b] view will appear
2013-12-12 12:33:47.854 moveplanner[7604:70b] tableView frame height 648.000000
2013-12-12 12:33:47.854 moveplanner[7604:70b] tableView frame width 320.000000
2013-12-12 12:33:47.855 moveplanner[7604:70b] tableView contentSize height 2313.058105
2013-12-12 12:33:47.856 moveplanner[7604:70b] tableView contentSize width 320.000000
2013-12-12 12:33:47.856 moveplanner[7604:70b] tableView content offset x 0.000000 - y 0.000000

2013-12-12 12:33:47.934 moveplanner[7604:70b] viewDidAppear details
2013-12-12 12:33:55.470 moveplanner[7604:70b] UPDATE TASK
2013-12-12 12:33:55.471 moveplanner[7604:70b] SELECTED TASK : Switch home insurance
2013-12-12 12:33:55.494 moveplanner[7604:70b] tableView frame height 808.000000
2013-12-12 12:33:55.495 moveplanner[7604:70b] tableView frame width 320.000000
2013-12-12 12:33:55.496 moveplanner[7604:70b] tableView contentSize height 2313.058105
2013-12-12 12:33:55.496 moveplanner[7604:70b] tableView contentSize width 320.000000
2013-12-12 12:33:55.496 moveplanner[7604:70b] tableView content offset x 0.000000 - y 0.000000
First click on a cell

2013-12-12 12:33:57.546 moveplanner[7604:70b] UPDATE TASK
2013-12-12 12:33:57.546 moveplanner[7604:70b] SELECTED TASK : Recycle unwanted items
2013-12-12 12:33:57.547 moveplanner[7604:70b] tableView frame height 968.000000
2013-12-12 12:33:57.548 moveplanner[7604:70b] tableView frame width 320.000000
2013-12-12 12:33:57.549 moveplanner[7604:70b] tableView contentSize height 2313.058105
2013-12-12 12:33:57.549 moveplanner[7604:70b] tableView contentSize width 320.000000
2013-12-12 12:33:57.549 moveplanner[7604:70b] tableView content offset x 0.000000 - y 317.000000


Second click on a cell

2013-12-12 12:34:00.448 moveplanner[7604:70b] UPDATE TASK
2013-12-12 12:34:00.448 moveplanner[7604:70b] SELECTED TASK : Gas boiler service
2013-12-12 12:34:00.449 moveplanner[7604:70b] tableView frame height 1128.000000
2013-12-12 12:34:00.450 moveplanner[7604:70b] tableView frame width 320.000000
2013-12-12 12:34:00.451 moveplanner[7604:70b] tableView contentSize height 2313.058105
2013-12-12 12:34:00.451 moveplanner[7604:70b] tableView contentSize width 320.000000
2013-12-12 12:34:00.451 moveplanner[7604:70b] tableView content offset x -0.000000 - y 1185.000000

Third click on a cell

2013-12-12 12:34:09.852 moveplanner[7604:70b] UPDATE TASK
2013-12-12 12:34:09.852 moveplanner[7604:70b] SELECTED TASK : Change the locks 
2013-12-12 12:34:09.853 moveplanner[7604:70b] tableView frame height 1288.000000
2013-12-12 12:34:09.854 moveplanner[7604:70b] tableView frame width 320.000000
2013-12-12 12:34:09.854 moveplanner[7604:70b] tableView contentSize height 2313.058105
2013-12-12 12:34:09.855 moveplanner[7604:70b] tableView contentSize width 320.000000
2013-12-12 12:34:09.855 moveplanner[7604:70b] tableView content offset x -0.000000 - y 1025.000000

2 个答案:

答案 0 :(得分:0)

    [self.tableView scrollToRowAtIndexPath:cellAtIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

可能是罪魁祸首。

答案 1 :(得分:0)

我找到了一个解决方案。我不知道这是解决问题的正确方法,但它确实有效。

我在NSIndexPath *cellAtIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];[self.tableView scrollToRowAtIndexPath:cellAtIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

之后添加了didSelectRowAtIndexPath
                CGRect tbFrame = [self.tableView frame];
                if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
                {
                    tbFrame.size.height = 910;                    }
                else {
                     tbFrame.size.height = 655;

                }
[self.tableView setFrame:tbFrame];

希望能帮助一些人