Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 如何使我的NSCollectionView项目可选?_Cocoa_Binding - Fatal编程技术网

Cocoa 如何使我的NSCollectionView项目可选?

Cocoa 如何使我的NSCollectionView项目可选?,cocoa,binding,Cocoa,Binding,在awakeFromNib中,我有: [projectArrayController addObserver:self forKeyPath:@"selectionIndexes" options:NSKeyValueObservingOptionNew context:nil]; 我有: - (void)observeValueForKeyPath:(NSStr

awakeFromNib
中,我有:

[projectArrayController addObserver:self
                    forKeyPath:@"selectionIndexes"
                       options:NSKeyValueObservingOptionNew
                       context:nil];
我有:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {
    NSLog(@"%@ forObject: %@",keyPath, object);
    if([keyPath isEqualTo:@"selectionIndexes"]){
        NSUInteger numberOfSelected = [[projectArrayController selectedObjects] count];
        if(numberOfSelected >0){
            if (numberOfSelected == 1){
                ProjectModel *pm =  (ProjectModel *)[[projectArrayController selectedObjects] objectAtIndex:0];
                [pm setSelected:YES];
            }
        }
    }
}
哪些日志:
selectionIndexes for object:[对象类:ProjectModel,所选对象数:1]

但是当我运行我的程序时,我实际上不能点击其他任何东西。为什么会这样? 我应该使用哪种类型的委托?到目前为止,我尝试使用tableview委托和collectionview委托


或者,我如何让我的NSCollection View成为第一响应者?

我已经做到了,但这并不容易。您必须对集合视图的子视图进行子类化,该子视图是NSView的子类。下面是我的子类的样子

@implementation GiggleCollectionSubView
@synthesize itIsSelected;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        itIsSelected = NO;
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)awakeFromNib {

}

- (void)drawRect:(NSRect)rect {
    [[NSColor clearColor] set];
    if (self.itIsSelected) {
        [NSGraphicsContext saveGraphicsState];
        NSSetFocusRingStyle(NSFocusRingAbove);
        [[NSBezierPath bezierPathWithRect:NSInsetRect([self bounds], 2, 2)] fill];
        [NSGraphicsContext restoreGraphicsState];
    } else {
        [[NSBezierPath bezierPathWithRect:[self bounds]] fill];
    }
}

- (void)mouseDown:(NSEvent *)theEvent {
    if ([theEvent clickCount]==1) {
        if (![self itIsSelected]) {
            [[[NSApp delegate] giggleWindowController] clearCollectionView];
            [self setItIsSelected:YES];
            [self setNeedsDisplay:YES];
            [[[NSApp delegate] giggleWindowController] updateSelectionIndexes];
            [[[NSApp delegate] giggleWindowController] showSelectedImage];
        }
    }
}

@end
您可以看到,我有一个名为itIsSelected的实例变量。这是关键。我手动管理变量,使一切正常。首先,在drawRect方法中,它确定子视图是否绘制了一个焦点环,以指示选择了一个子视图。mouseDown:方法的下一步是当用户单击子视图以选择它时进行检测。在这里,我手动管理阵列控制器的SelectionIndex,并手动管理所有子视图的itIsSelected实例变量。因此,首先我为方法clearCollectionView中的所有子视图将其设置为“否”。接下来,对于单击的子视图,我将其设置为“是”。然后,我更新阵列控制器的SelectionIndex,然后根据所选对象执行某些操作(例如,在showSelectedImage中)。以下是这两种方法

-(void)clearCollectionView {
    NSArray* cvViews = [artistImagesCV subviews];
    for (GiggleCollectionSubView* aView in cvViews) {
        if ([aView itIsSelected]) {
            [aView setItIsSelected:NO];
            [aView setNeedsDisplay:YES];
        }
    }
}

-(void)updateSelectionIndexes {
    NSArray* cvViews = [artistImagesCV subviews];
    NSMutableIndexSet* indexes = [NSMutableIndexSet indexSet];

    NSUInteger counter = 0;
    for (GiggleCollectionSubView* aView in cvViews) {
        if ([aView itIsSelected]) [indexes addIndex:counter];
        counter++;
    }
    [googleImagesArrayController setSelectionIndexes:(NSIndexSet*)indexes];
}
注意:在我的子视图中,我还有一个图像视图。我必须将其子类化,并在mouseDown方法中执行类似的操作


不管怎样,我希望这有帮助。我无法使用您尝试的方法使其工作,因此我按照说明手动执行。祝你好运。

谢谢你花时间回答,但我没有对任何子类化就解决了这个问题。我对视图上的NSBox使用了这种方法,只是改变了它的颜色(这是我所需要的,但是)。