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