插入行时保持相同的NSTableView滚动位置 [英] Keep same NSTableView scrolled position while inserting rows

查看:200
本文介绍了插入行时保持相同的NSTableView滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于视图的NSTableView,它显示消息/信息的时间轴。行高是可变的。新消息通常使用 insertRows 添加到表顶部:

I've got a view-based NSTableView showing a timeline of messages/informations. Row heights are variable. New messages are regularly added at the top of the table by using insertRows:

NSAnimationContext.runAnimationGroup({ (context) in
    context.allowsImplicitAnimation = true
    self.myTable.insertRows(at: indexSet, withAnimation: [.effectGap])
})

当用户停留在表的顶部时,消息将继续插入

While the user stays at the top of the table, the messages keep being inserted at the top, pushing down the existing ones: a usual behavior in this context.

一切正常,除了如果用户向下滚动,则插入新内容消息不应该使表格滚动。

Everything works ok, except that if the user has scrolled down, the new inserted messages shouldn't make the table scroll.

我希望tableView保持在用户滚动或用户向下滚动时的位置。

I want the tableView to stay where it is while the user scrolls or if the user has scrolled down.

换句话说,只有在第一行可见100%的情况下,才应由新插入的行向下推tableView。

我试图给人一种错觉,即桌子不能快速移动像这样存储其位置:

I've tried to give the illusion of the table not moving by quickly restoring its location like this:

// we're not at the top anymore, user has scrolled down, let's remember where
let scrollOrigin = self.myTable.enclosingScrollView!.contentView.bounds.origin
// stuff happens, new messages have been inserted, let's scroll back where we were
self.myTable.enclosingScrollView!.contentView.scroll(to: scrollOrigin)

但是它的行为并不像我想要的那样。我已经尝试了许多组合,但是我认为我对剪辑视图,滚动视图和表格视图之间的关系不了解。

But it doesn't behave as I want. I've tried many combinations but I think I'm not understanding something about the relation between the clip view, the scroll view and the table view.

或者也许我

推荐答案

忘记了滚动视图,剪辑视图, contentView,documentView并集中在表视图上。表格视图可见部分的底部不应移动。您可能已经错过了翻转的坐标系。

Forget the scroll view, clip view, contentView, documentView and focus on the table view. The bottom of the visible part of the table view shouldn't move. You might have missed the flipped coordinate system.

NSPoint scrollOrigin;
NSRect rowRect = [self.tableView rectOfRow:0];
BOOL adjustScroll = !NSEqualRects(rowRect, NSZeroRect) && !NSContainsRect(self.tableView.visibleRect, rowRect);
if (adjustScroll) {
    // get scroll position from the bottom: get bottom left of the visible part of the table view
    scrollOrigin = self.tableView.visibleRect.origin;
    if (self.tableView.isFlipped) {
        // scrollOrigin is top left, calculate unflipped coordinates
        scrollOrigin.y = self.tableView.bounds.size.height - scrollOrigin.y;
    }
}

// insert row
id object = [self.arrayController newObject];
[object setValue:@"John" forKey:@"name"];
[self.arrayController insertObject:object atArrangedObjectIndex:0];

if (adjustScroll) {
    // restore scroll position from the bottom
    if (self.tableView.isFlipped) {
        // calculate new flipped coordinates, height includes the new row
        scrollOrigin.y = self.tableView.bounds.size.height - scrollOrigin.y;
    }
    [self.tableView scrollPoint:scrollOrigin];
}

我没有测试过 tableView是否保留在用户所处的位置卷轴。

I didn't test "the tableView to stay where it is while the user scrolls".

这篇关于插入行时保持相同的NSTableView滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆