2016-11-28 1 views
0

私は並べ替えるNSCollectionViewを持っています。 Flow Layout、Delegate、およびDatasourceを使用していますが、ViewControllerに設定されています。 は、私も自分のドラッグタイプを登録しているが、私は唯一のためのデリゲートの呼び出しを取得:他のデリゲートの呼び出しのためNSCollectionViewの並べ替え(フローレイアウト)

- (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event; 

でもありません。ここに私のViewControllerのソースコードがあります:

#import "ViewController.h" 
#import "CollectionViewItem.h" 

@interface ViewController() 

@property(nonatomic, strong) NSArray *strings; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do view setup here. 
} 


- (void)awakeFromNib 
{ 
    self.strings = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h"]; 

    NSArray *supportedTypes = [NSArray arrayWithObjects:@"CustomDDType", nil]; 
    [self.collectionView registerForDraggedTypes:supportedTypes]; 
} 


#pragma mark CollectionView DataSource 


- (NSInteger) numberOfSectionsInCollectionView:(NSCollectionView *)collectionView 
{ 
    return 1; 
} 


- (NSInteger) collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return self.strings.count; 
} 



- (NSCollectionViewItem *) collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CollectionViewItem *item = [collectionView makeItemWithIdentifier:@"CollectionViewItem" forIndexPath:indexPath]; 
    item.myTitle.stringValue = [self.strings objectAtIndex:indexPath.item]; 

    return item; 
} 

#pragma mark Drag/Drop 

- (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event { 
    NSLog(@"canDragItems"); 
    return YES; 
} 


-(NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id<NSDraggingInfo>)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation { 
    NSLog(@"Validate Drop"); 
    return NSDragOperationMove; 
} 


-(BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard 
{ 
    NSLog(@"Write Items at indexes : %@", indexes); 

    NSData *indexData = [NSKeyedArchiver archivedDataWithRootObject:indexes]; 
    [pasteboard declareTypes:@[@"CustomDDType"] owner:self]; 
    [pasteboard setData:indexData forType:@"CustomDDType"]; 

    return YES; 
} 


- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id<NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation { 
    NSLog(@"Accept Drop"); 

    NSPasteboard *pBoard = [draggingInfo draggingPasteboard]; 
    NSData *indexData = [pBoard dataForType:@"CustomDDType"]; 
    NSIndexSet *indexes = [NSKeyedUnarchiver unarchiveObjectWithData:indexData]; 
    NSInteger draggedCell = [indexes firstIndex]; 

    return YES; 
} 

@end 

私は何かを見逃しましたか?プロジェクトを以前に実行する必要があるため、10.11で導入された新しいデリゲートコールを使用することはできません。

+1

10.11より前にフローレイアウトを使用できますか? – Willeke

+0

そのヒントありがとうございます。私はそれが不可能だと思う。現在、コレクションビューをコンテンツ配列のレイアウトに変更しています。 – Matt

+0

私は同じ問題があり、それは私を怒らせます。その間に何か解決策を見つけましたか? – JFS

答えて

0

OK!だからあなたはフローレイアウトを再注文しています!これはトリッキーなことができる - 私はので、私はあなたに[概要]

任意のテーブル/コレクションを並べ替え、あなたが複数の変更がある場合、あなたは、データソースへのバッチ更新に必要を与える私のMac上ではないです。 (あなたはすでに知っているかもしれません)

データソースを接続してビューに委任してください。 (私はコードでそれを見ることはできませんので、あなたはそれをインターフェイスビルダーでやったと思います)

最後に、コレクションビューがStrong(とても強い)であることを確認したい場合があります。 - この作品のどれも私に知らせていない場合。

+0

ありがとうございます。はい、データソースに接続し、インタフェースビルダーのビューコントローラに委任しました。たぶん私はフローレイアウトが必要ではない。並べ替えのレイアウトとしてContent Arrayを使用する方が簡単ですか?しかし、私はcanDragItemsAtIndexデリゲートコールを取得するだけです。 – Matt

+0

マット - デリゲートプロトコルが設定されていますか? ViewController:NSCollectionViewDelegate –

+0

はい、デリゲートプロトコルを設定しました。 – Matt

関連する問題