2016-11-28 133 views
0

我有一個NSCollectionView,我想重新排序。我使用流佈局,委託和數據源設置爲我的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,所以我會給你'概述'

當重新排序任何表/集合,並且您有多個更改時,您需要批量更新到數據源。 (您可能已經知道)

確保將您的數據源和委託連接到視圖。 (我沒有在代碼中看到它,所以我想你是在界面生成器中做的)

最後,你可能想檢查一下你的集合視圖是強的(如此強大) - 因爲在它顯示之前它可能會丟失它的引用。 - 如果沒有這個作品讓我知道。

+0

謝謝你的回答。是的,我將數據源和委託連接到界面構建器中的視圖控制器。也許我不需要流佈局。使用Content Array作爲佈局重新排序更容易嗎?但我只獲得canDragItemsAtIndex委託調用。 – Matt

+0

Matt - 你有Delegate協議集嗎? ViewController:NSCollectionViewDelegate –

+0

是的,我確實設置了委託協議。 – Matt