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

前端之家收集整理的这篇文章主要介绍了macos – 动画完成后如何滚动到添加到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];

我怎样才能做到这一点?

解决方法

我找到了解决他的问题的方法,我很满意:

[_tableView insertRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationEffectGap];

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    NSUInteger firstIndex = [indexSet firstIndex];
    [_tableView scrollRowToVisible:firstIndex];
}];

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

猜你在找的cocoa相关文章