如何让 NSTableView 滚动到最近添加的行?

发布于 2024-08-12 02:43:28 字数 99 浏览 5 评论 0原文

我正在向 NSTableView 动态添加行。当我发出 [table reloadData] 时,我可以看到滚动视图移动,如果我手动移动它,我可以看到表上的新值。但如何才能自动滚动呢?

I'm dynamically adding rows to an NSTableView. When I issue [table reloadData] I can see the scroll view move and if I move it by hand I can see the new value on the table. But how can I scroll it automatically?

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

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

发布评论

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

评论(4

撩起发的微风 2024-08-19 02:43:28
NSInteger numberOfRows = [tableView numberOfRows];

if (numberOfRows > 0)
    [tableView scrollRowToVisible:numberOfRows - 1];

假设您要将行添加到表的末尾。

(我向表视图询问行数而不是数据源的原因是,这个数字保证是它知道并且可以滚动到的行数。)

NSInteger numberOfRows = [tableView numberOfRows];

if (numberOfRows > 0)
    [tableView scrollRowToVisible:numberOfRows - 1];

Assuming you're adding rows to the end of the table.

(The reason I ask the table view for the number of rows instead of the data source is that this number is guaranteed to be the number of rows it knows about and can scroll to.)

じ违心 2024-08-19 02:43:28

使用这样的东西:

 [table scrollRowToVisible:indexOfNewRow];

Use something like this:

 [table scrollRowToVisible:indexOfNewRow];
风筝有风,海豚有海 2024-08-19 02:43:28

我解决了它,如下所示,使用 didAddRowView 正常工作:
显然,如果我添加一条新记录,表视图需要创建一个新的行视图。

- (void)tableView:(NSTableView *)tableView  didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
// it sends notification from all tableviews.
// I am only interested in the dictionary...

if (tableView == dictTableView){
    if ( row == [dictTableView numberOfRows]-1){
        [dictTableView scrollRowToVisible:row];
    }
}

而且

,在我的 IBAction 中,我在对控制器执行 add 语句后添加了 2 行

- (IBAction)addNewDictItem:(id)sender {
NSInteger row=[[theDictArrayController arrangedObjects]count];

[theDictArrayController add:sender];
[dictTableView noteNumberOfRowsChanged];
[dictTableView scrollRowToVisible:row-1];

}

I solved it as shown below, using the didAddRowView to work properly:
Obviously, if I add a new record, the tableview needs to create a new row view.

- (void)tableView:(NSTableView *)tableView  didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
// it sends notification from all tableviews.
// I am only interested in the dictionary...

if (tableView == dictTableView){
    if ( row == [dictTableView numberOfRows]-1){
        [dictTableView scrollRowToVisible:row];
    }
}

}

But also, in my IBAction I added 2 lines after performing the add statement to the controller

- (IBAction)addNewDictItem:(id)sender {
NSInteger row=[[theDictArrayController arrangedObjects]count];

[theDictArrayController add:sender];
[dictTableView noteNumberOfRowsChanged];
[dictTableView scrollRowToVisible:row-1];

}

对岸观火 2024-08-19 02:43:28

下面显示了对我之前的解决方案的改进解决方案。捕获“didAddRow”事件可能还为时过早,因此新行(滚动到...)尚不可用。

首先,创建您自己的“添加”操作,保留一个标记作为提醒并调用控制器的添加操作。

-(IBAction)addNewDictItem:(id)sender {
   [theDictArrayController add:sender];
   dictElementAdded=TRUE;
 }

现在,当控制器添加一个对象时,它会自动选择该对象。
只需通过从表格视图捕获通知来“跳上”此事件即可。
这样做的好处是:线条和视图之前已创建,并且新行已准备好可供选择。

-(void)tableViewSelectionDidChange:(NSNotification *)aNotification {
NSTableView * tableView=[aNotification object];

if (dictElementAdded && tableView == dictTableView  ){
    NSInteger   row= [[tableView selectedRowIndexes ] firstIndex ];        
        [dictTableView scrollRowToVisible:row];
        dictElementAdded = FALSE;
 }
}

A improved solution to my previous one is shown below. Catching the "didAddRow" event is probably still too early, so that the new row (to scroll to...) is not available yet.

First, create your own "add" action , keep a flag as reminder and call the controller's add action.

-(IBAction)addNewDictItem:(id)sender {
   [theDictArrayController add:sender];
   dictElementAdded=TRUE;
 }

Now, when the controller adds an object, it selects this object automatically.
Just "hop on" this event by catching the notification from the tableview.
Nice thing about it: lines and views are created before, and the new row is ready to be selected.

-(void)tableViewSelectionDidChange:(NSNotification *)aNotification {
NSTableView * tableView=[aNotification object];

if (dictElementAdded && tableView == dictTableView  ){
    NSInteger   row= [[tableView selectedRowIndexes ] firstIndex ];        
        [dictTableView scrollRowToVisible:row];
        dictElementAdded = FALSE;
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文