2016-11-28 134 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