【问题标题】:selectRowAtIndexPath failing in viewWillAppearselectRowAtIndexPath 在 vi​​ewWillAppear 中失败
【发布时间】:2011-09-28 22:06:41
【问题描述】:

I've got a menu that's a UITableview in a UIPopovercontroller that when selected scrolls the parent view's UIScollView to a specific frame.

效果很好。

问题是如果你使用pageControl滚动框架我需要更新表格中选中的行[_delegate returnPageNumber]返回当前pageControl.currentPage

没有错误,NSLog 正在报告正确的页码:

scrollIndexPath is <NSIndexPath 0x1a3380> 2 indexes [0, 3]

但是正确的单元格没有突出显示...为什么????

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //[tableView reloadData];
    int isPage = [_delegate returnPageNumber];
    NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:(isPage) inSection:0];
    NSLog(@"scrollIndexPath is %@",scrollIndexPath);
    [tableView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];      
}

我尝试在之前和之后放置 [tableView reloadData] 并将代码放在 viewDidAppear 中......没有任何效果

【问题讨论】:

  • 你为什么不使用 -didSelectRowAtIndexPath?

标签: ios ipad uitableview uipopovercontroller


【解决方案1】:

尝试在其他所有内容之后调用[super viewWillAppear],在viewWillAppear

- (void)viewWillAppear:(BOOL)animated 
{

    NSIndexPath *selection = [self.tableView indexPathForSelectedRow];

    [[self tableView] reloadData];    


    if (selection)
    {
        [[self tableView] selectRowAtIndexPath:selection animated:NO scrollPosition:UITableViewScrollPositionNone];        
    }

    [super viewWillAppear:animated]; //Trick is calling super last in this case. Then you can  retrieve previously selected row to --> NSIndexPath *selection

}

【讨论】:

    【解决方案2】:

    现在开始工作了!

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        [[self tableView] reloadData];
    
        int isPage = [_delegate returnPageNumber];
    
        NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:(isPage) inSection:0];
        NSLog(@"viewDidAppear scrollIndexPath is %@",scrollIndexPath);
    
        [[self tableView] selectRowAtIndexPath:scrollIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
    

    造成差异的事情是断开视图出口,然后将其重新连接到 tableview。不知道为什么这会有所作为?我尝试将视图控制器添加到笔尖,但没有任何区别,因此删除它并将视图重新连接到 tableview 突然产生了结果。巫毒教。

    【讨论】:

      猜你喜欢
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      相关资源
      最近更新 更多