【问题标题】:Nested NSNotification for NSTableViewsNSTableViews 的嵌套 NSNotification
【发布时间】:2013-09-23 15:18:51
【问题描述】:

我有两个表视图来控制对象集合的显示/排序(即按类别和本地化,即 Lieu)。 我的问题是:我希望在用户单击任何这些表视图中的单元格时更新选择(使用 NSTableViewDelegate 可以正常工作),但我也想将选择恢复为另一个表中的默认值查看。

我的问题很明显:每次调用 tableViewSelectionDidChange 都会触发对自己的另一个调用,这使得结果非常不稳定。有没有办法阻止这个电话[tableViewCategory selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO]; 触发通知?

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{   

    if ([[[aNotification object]identifier]isEqualToString:@"table2"]){
        //First, reset AnnonceWithCategory
        [tableViewCategory selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
        //Then
        [self showAnnoncesWithLieu];
    }
    else if ([[[aNotification object]identifier]isEqualToString:@"table3"]){
        //First, reset AnnonceWithLieu
        [tableViewLieu selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
        [self showAnnoncesWithCategory];
    }
}

【问题讨论】:

    标签: objective-c cocoa nstableview


    【解决方案1】:

    您无法阻止NSTableView 发送通知,但您可以阻止您的班级对其做出响应。你可以这样做:

    - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
    {   
    
        if ([[[aNotification object]identifier]isEqualToString:@"table2"] && ! _currentlyUpdatingTable2){
            //First, reset AnnonceWithCategory
            _currentlyUpdatingTable2 = YES;
            [tableViewCategory selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
            _currentlyUpdatingTable2 = NO;
            //Then
            [self showAnnoncesWithLieu];
        }
        else if ([[[aNotification object]identifier]isEqualToString:@"table3"] && ! _currentlyUpdatingTable3){
            //First, reset AnnonceWithLieu
            _currentlyUpdatingTable3 = YES;
            [tableViewLieu selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
            _currentlyUpdatingTable3 = NO;
            [self showAnnoncesWithCategory];
        }
    }
    

    ..._currentlyUpdatingTable2_currentlyUpdatingTable3 是接收通知的对象的 ivars。

    【讨论】:

    • 像魅力一样工作;虽然你有一个小的逻辑错误,我在你的回答中修复了(! _currentlyUpdatingTable3! (_currentlyUpdatingTable3 || _currentlyUpdatingTable2)。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多