2012-01-09 56 views
3

我在可可應用程序中設置了一個NSCollectionView。當我選擇/取消選擇其中一個視圖時,我已將分類視圖的NSCollectionViewItem分類爲自定義NSNotification。當我發佈此通知時,我註冊接收控制器對象內的通知。在這種方法中,我告訴剛被選中的視圖,並選擇它並重新繪製,這會使它變成灰色。在NSCollectionView中創建沒有空選擇

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發生空選擇?我在界面構建器中看不到任何複選框。

在此先感謝!

更新

我結束了剛剛繼承我的NSCollectionView,並重寫- (void)mouseDown:(NSEvent *)theEvent方法。如果點擊是在其中一個子視圖中,我才發送方法[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];如果點擊是在其中一個子視圖中。代碼:

- (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

我也想在我的收藏視圖中避免空選擇。
我做它的方式也由子類,但我推翻-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; 
} 
0

雖然我來晚了此主題,我因爲我最近也遇到了同樣的問題,所以我只是想侃侃而談。我用下面的代碼行了周圍:

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

有一點需要注意:在avoidsEmptySelection屬性是不是官方API的一部分,但我認爲這是相當安全的假設,物業公司的類型會堅持一段時間。

+0

我想你的意思是[_collectionView setValue:@YES forKey:@「avoidsEmptySelection」] – 2014-02-06 11:22:52

+0

當然是的:) – aLevelOfIndirection 2014-02-11 10:37:15

相關問題