1

我想在 NSTableView 中选择特定记录时触发查询,而不是当用户通过连续按下向上/向下按钮来向下滚动或向上滚动以到达记录时。

我目前的实现是

if ([notification object] == myTableView)
    {
        if ([myTableView selectedRow] >= 0) {
            myCont = [[MyController alloc] init];
            if([[detailsView subviews]count]>0)
                [detailsView removeAllSubviews];
            NSRect frameRect = [[scDetailsViewController view] frame];
            frameRect.size.height = [detailsView frame].size.height;
            frameRect.size.width = [detailsView frame].size.width;
            [[myCont view] setFrame:frameRect];
            [detailsView addSubview:[myCont view]];
//Firing the Query
            [myCont populateDetails :[[self myList] entityAt:[myTableView selectedRow]]];
        }
}

但是以这种方式,即使长时间按下 UP/DOWN,查询也会被触发,这不是预期的。

有什么方法可以区分 NSTableView 中的单击和滚动(连续按下 UP/DOWN 键),就像邮件应用程序一样。

4

2 回答 2

0

只需在方法中编写以下代码tableViewSelectionDidChange,几乎可以完美运行。

- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
    if ([notification object] == myTableView)
    {
        NSTimeInterval delayInterval = 0.0;
        NSEvent *event = [NSApp currentEvent];
        if(event != nil && [event type] == NSKeyDown && [event isARepeat])
        {
            NSLog(@"Long press of UP and DOWn arrow key.");
            delayInterval = [NSEvent keyRepeatInterval] + 0.01;
        }
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myMethod) object:nil];
        [self performSelector:@selector(myMethod) withObject:nil afterDelay:delayInterval];
    }
    else
    {
        if ([[detailsView subviews] count]>0)
        {
            [detailsView removeAllSubviews];
        }
    }
}

还编写了代码以在myMethod上面调用的另一种方法中触发查询performSelector

-(void) myMethod
{
    if ([scenarioTableView selectedRow] >= 0) {
        NSLog(@"Normal selection on table view row.");
        scDetailsViewController = [[ScenarioDetailsViewController alloc] init];
        if([[detailsView subviews]count]>0)
            [detailsView removeAllSubviews];
        NSRect frameRect = [[scDetailsViewController view] frame];
        frameRect.size.height = [detailsView frame].size.height;
        frameRect.size.width = [detailsView frame].size.width;
        [[scDetailsViewController view] setFrame:frameRect];
        [detailsView addSubview:[scDetailsViewController view]];
        [scDetailsViewController populateScenarioDetails :[[self scenarioDetailsList] entityAt:[scenarioTableView selectedRow]]];
    }
    else {
        if ([[detailsView subviews] count]>0)
        {
            [detailsView removeAllSubviews];
        }
    }
}
于 2013-10-08T10:26:46.323 回答
0

修改您的关键事件代码,如下所示,如果您仍然遇到任何问题,请告诉我:-

NSInteger keyPress = [[NSApp currentEvent] type];
if (keyPress!=10)
{
}
    NSLog(@"mouseclick");
}
else{
    NSLog(@"keyboardClick");
}
于 2013-10-08T11:09:26.323 回答