我在可可應用程序中設置了一個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];
}}
我想你的意思是[_collectionView setValue:@YES forKey:@「avoidsEmptySelection」] – 2014-02-06 11:22:52
當然是的:) – aLevelOfIndirection 2014-02-11 10:37:15