使用(流程布局)重新排序NSCollectionView

时间:2016-11-28 16:34:19

标签: objective-c macos drag-and-drop nscollectionview

我有一个NSCollectionView,我想重新排序。我正在使用Flow Layout,Delegate和Datasource设置为我的ViewController。 我也注册了我的拖拽类型,但我只收到了代理人的电话:

- (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 个答案:

答案 0 :(得分:0)

行!!!所以你正在重新订购流程布局!这可能很棘手 - 我不在我的Mac上,所以我会给你一个&#39;概述&#39;

重新排序任何表/集合并进行多项更改时,您需要批量更新到数据源。 (你可能已经知道了)

确保将数据源和委托连接到视图。 (我不会在代码中看到它,所以我想你是在界面构建器中做到的)

最后,您可能需要检查您的集合视图是否强大(如此强大) - 因为它可能会在显示之前丢失其引用。 - 如果这些都没有让我知道。