我有一個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版中引入的新委託調用,因爲我的項目需要先運行。
你可以在10.11之前使用流佈局嗎? – Willeke
謝謝你的提示。我認爲這是不可能的。目前將收藏視圖更改爲內容數組佈局。 – Matt
我有同樣的問題,它讓我發瘋。您在此期間是否找到任何解決方案? – JFS