2012-01-09 9 views
3

ココアアプリケーションにNSCollectionViewを設定しました。コレクションビューのNSCollectionViewItemをサブクラス化して、ビューの1つが選択/選択解除されたときにカスタムNSNotificationを送信しました。私は、この通知が掲示されたときに私のコントローラオブジェクト内で通知を受信するために登録する。このメソッドの中で、私は選択されたばかりのビューを選択し、再描画するように指示します。これにより、それ自体がグレーになります。NSCollectionViewで空の選択項目を作成しない

NSCollectionViewItemサブクラス:(-(void)awakeFromNib方法で)

-(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]];} 

ザ・コントローラクラス:

//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で空の選択を防止する簡単な方法があるのでしょうか?私はインターフェイスビルダーでチェックボックスを参照してください。

ありがとうございます!

ベン

更新

私はちょうど私のNSCollectionViewをサブクラス化し、- (void)mouseDown:(NSEvent *)theEventメソッドをオーバーライドすることになりました。クリックは、サブビューの1にあった場合、私は、メソッド[super mouseDown:theEvent];を送りました。コード:

- (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]; 
}} 

答えて

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]; 
}} 
0

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

-(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; 
} 
0

私はこのスレッドへの遅刻が、私は私は最近同じ問題があったので、私はちょうどチャイムインすると思った。私は、次のコード行を使用して、その周りになった:

[_collectionView setValue:@NO forKey:@"avoidsEmptySelection"]; 

1つの注意点があります:私はそれを想定してかなり安全だと思いますが、avoidsEmptySelectionプロパティは、公式APIの一部ではない財産のそのタイプのものということしばらくの間にこだわる。

+0

あなたは[_collectionView setValue:@YES forKey:@ "avoidsEmptySelection"]を意味すると思います –

+0

もちろんあります:) – aLevelOfIndirection

関連する問題