3

NSCollectionViewココア アプリケーションでをセットアップしました。ビューの1つが選択/選択解除されたときにNSCollectionViewItemカスタムを送信するために、コレクションビューをサブクラス化しました。NSNotificationこの通知が投稿されたときに、コントローラー オブジェクト内で通知を受け取るように登録します。このメソッド内で、選択されたばかりのビューにそれが選択されていることを伝え、再描画するように伝えます。これにより、それ自体が灰色になります。

NSCollectionViewItemサブクラス:

-(void)setSelected:(BOOL)flag {
[super setSelected:flag];

[[NSNotificationCenter defaultCenter] postNotificationName:@"ASCollectionViewItemSetSelected" 
                                                    object:nil 
                                                  userInfo:[NSDictionary dictionaryWithObjectsAndKeys:(ASListView *)self.view, @"view", 
                                                            [NSNumber numberWithBool:flag], @"flag", nil]];}

コントローラ クラス (-(void)awakeFromNibメソッド内):

//Register for selection changed notification
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(selectionChanged:) 
                                             name:@"ASCollectionViewItemSetSelected" 
                                           object:nil];

そして-(void)selectionChanged:(NSNotification *)notification方法:

- (void)selectionChanged:(NSNotification *)notification {
// * * Must get the selected item and set its properties accordingly

//Get the flag
NSNumber *flagNumber = [notification.userInfo objectForKey:@"flag"];
BOOL flag = flagNumber.boolValue;

//Get the view
ASListView *listView = [notification.userInfo objectForKey:@"view"];

//Set the view's selected property
[listView setIsSelected:flag];
[listView setNeedsDisplay:YES];

//Log for testing
NSLog(@"SelectionChanged to: %d on view: %@", flag, listView);}

このコードを含むアプリケーションでは、コレクション ビュー内で常に空の選択がないようにする必要があります。ここで私の問題が発生します。ビューの選択がいつ変更されたかを確認し、選択がない場合は再選択し、を使用してビューを手動で選択しようとしましNSCollectionView

-(void)setSelectionIndexes:(NSIndexSet *)indexes

しかし、コレクション ビューで空の選択が行われる状況が常に発生します。

NSCollectionViewそれで、空の選択が?で発生するのを防ぐ簡単な方法があるかどうか疑問に思っていました。Interface Builder にチェックボックスがありません。

前もって感謝します!

ベン

アップデート

結局、 my をサブクラス化しNSCollectionView、メソッドをオーバーライドしました- (void)mouseDown:(NSEvent *)theEvent[super mouseDown:theEvent];クリックがサブビューの1つであった場合にのみ、メソッドを送信しました。コード:

- (void)mouseDown:(NSEvent *)theEvent {
NSPoint clickPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];

int i = 0;
for (NSView *view in self.subviews) {
    if (NSPointInRect(clickPoint, view.frame)) {
        //Click is in rect
        i = 1;
    }
}

//The click wasnt in any of the rects
if (i != 0) {
    [super mouseDown:theEvent];
}}
4

3 に答える 3

0

私は自分の NSCollectionView をサブクラス化し、- (void)mouseDown:(NSEvent *)theEventメソッドをオーバーライドするだけになりました。その後、メソッド [super mouseDown:theEvent]; を送信しました。クリックがサブビューの 1 つであった場合。コード:

- (void)mouseDown:(NSEvent *)theEvent {
NSPoint clickPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];

int i = 0;
for (NSView *view in self.subviews) {
    if (NSPointInRect(clickPoint, view.frame)) {
        //Click is in rect
        i = 1;
    }
}

//The click wasnt in any of the rects
if (i != 0) {
    [super mouseDown:theEvent];
}}
于 2012-01-16T14:07:25.870 に答える
0

また、コレクション ビューで空の選択を避けたいと考えていました。
私が行った方法もサブクラス化によるものですが、クリックが項目になかった場合に戻る-hitTest:代わりにオーバーライドしました:-mouseDown:nil

-(NSView *)hitTest:(NSPoint)aPoint {
    // convert aPoint in self coordinate system
    NSPoint localPoint = [self convertPoint:aPoint fromView:[self superview]];
    // get the item count
    NSUInteger itemCount = [[self content] count];

    for(NSUInteger itemIndex = 0; itemIndex < itemCount; itemIndex += 1) {
        // test the point in each item frame
        NSRect itemFrame = [self frameForItemAtIndex:itemIndex];
        if(NSPointInRect(localPoint, itemFrame)) {
            return [[self itemAtIndex:itemIndex] view];
        }
    }

    // not on an item
    return nil;
}
于 2013-08-21T08:56:05.173 に答える