动画完成后如何滚动到添加到 NSTableView 的行

tar*_*mes 5 macos cocoa core-animation nstableview

将新行添加到 NSTableView 后,我想滚动到它。

当该行被添加到表的末尾时,滚动仅滚动到之前是最后一行的行。我最初以为我必须等待动画完成,但这并没有解决我的问题。这是我的代码:

        [NSAnimationContext beginGrouping];
        [_tableView insertRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationEffectGap];
        [[NSAnimationContext currentContext] setCompletionHandler:^{

            // Scroll to the first inserted row

            NSUInteger firstIndex = [indexSet firstIndex];
            [_tableView scrollRowToVisible:firstIndex];

        }];
        [NSAnimationContext endGrouping];
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

tar*_*mes 3

我找到了一个令我满意的解决方案:

[_tableView insertRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationEffectGap];

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    NSUInteger firstIndex = [indexSet firstIndex];
    [_tableView scrollRowToVisible:firstIndex];
}];
Run Code Online (Sandbox Code Playgroud)

我只是将滚动请求延迟到下一个运行循环。

  • 在末尾插入一行后,我尝试使用它滚动到表视图的底部,它只是滚动到倒数第二行。有任何想法吗? (2认同)