【发布时间】:2016-11-28 16:34:19
【问题描述】:
我有一个 NSCollectionView,我想重新排序。我正在使用流布局,委托和数据源设置为我的 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 中引入的新委托调用,因为我的项目需要之前运行。
【问题讨论】:
-
10.11之前可以使用流式布局吗?
-
感谢您的提示。我认为这是不可能的。目前正在将集合视图更改为内容数组布局。
-
我也有同样的问题,这让我很生气。在此期间您是否找到任何解决方案?
标签: objective-c macos drag-and-drop nscollectionview