selectRowAtIndexPath 在 vi​​ewWillAppear 中失败

发布于 2024-12-06 22:43:53 字数 922 浏览 4 评论 0原文

我有一个菜单,它是 UIPopovercontroller 中的 UITableview,选择该菜单时会将父视图的 UIScollView 滚动到特定框架。

效果很好。

问题是,如果您使用 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 中...没有任何效果

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.

It's working great.

The problem is if you use the pageControl to scroll the frame I need to update the selected row in the table [_delegate returnPageNumber] returns the current pageControl.currentPage

No errors, NSLog is reporting the correct page number:

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

But the correct cell doesn't highlight... why????

- (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];      
}

I've tried putting [tableView reloadData] before and after and having the code in viewDidAppear... nothing works

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠魁 2024-12-13 22:43:53

尝试在 viewWillAppear 中的所有其他操作之后调用 [super 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

}

try calling [super viewWillAppear] after everything else, in 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

}
空城之時有危險 2024-12-13 22:43:53

现在工作了!

- (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];
}

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

Now working!

- (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];
}

The thing that made the difference was disconnecting the view outlet and then reconnecting it to the tableview. Not sure why this made a difference? I tried adding a view controller to the nib but it made no difference, so deleting it and reconnecting the view to the the tableview suddenly produced results. Voodoo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文