2014-04-25 42 views
6

Tôi đang cố gắng triển khai kéo thả trong NSCollectionView sẽ cho phép sắp xếp lại các ô trong chế độ xem. Tôi đã đặt đại biểu và thực hiện các phương thức bên dưới:Ví dụ Kéo thả NSCollectionView Ví dụ

-(BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard { 
    NSLog(@"Write Items at indexes : %@", indexes); 
    return YES; 
} 

- (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event { 
    NSLog(@"Can Drag"); 
    return YES; 
} 

- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id<NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation { 
    NSLog(@"Accept Drop"); 
    return YES; 
} 

-(NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id<NSDraggingInfo>)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation { 
    NSLog(@"Validate Drop"); 
    return NSDragOperationMove; 
} 

Tôi không chắc chắn cách thực hiện thêm. Với điều này tôi có thể thấy rằng bây giờ tôi có thể kéo xung quanh mục Bộ sưu tập cá nhân nhưng làm cách nào tôi có thể thực hiện Drop?

+0

xin chào user88975, bạn đã từng giải quyết vấn đề của mình chưa? Tôi đang đối mặt chính xác cùng một vấn đề và tôi không thể tìm thấy bất kỳ trợ giúp nào. – JFS

Trả lời

6

Bạn chỉ thực hiện các phương thức ủy nhiệm nhưng không có logic trong một số phương pháp. Ví dụ để kéo xung quanh một Bộ sưu tập mục I sẽ thêm bên dưới logic:

-(BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard { 
    NSData *indexData = [NSKeyedArchiver archivedDataWithRootObject:indexes]; 
    [pasteboard setDraggedTypes:@["my_drag_type_id"]]; 
    [pasteboard setData:indexData forType"@"my_drag_type_id"]; 
    // Here we temporarily store the index of the Cell, 
    // being dragged to pasteboard. 
    return YES; 
} 

- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id<NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation { 
    NSPasteboard *pBoard = [draggingInfo draggingPasteboard]; 
    NSData *indexData = [pBoard dataForType:@"my_drag_type_id"]; 
    NSIndexSet *indexes = [NSKeyedUnarchiver unarchiveObjectWithData:indexData]; 
    NSInteger draggedCell = [indexes firstIndex]; 
    // Now we know the Original Index (draggedCell) and the 
    // index of destination (index). Simply swap them in the collection view array. 
    return YES; 
} 

Bạn cũng cần phải đăng ký xem bộ sưu tập để kéo gõ awakeFromNib như

[_myCollectionView registerForDraggedTypes:@[KL_DRAG_TYPE]]; 

Và chắc chắn rằng bạn đã thiết lập chế độ xem bộ sưu tập là có thể chọn.

+0

GoodSp33d, loại 'KL_DRAG_TYPE' là gì không hợp lệ trong Swift 3? – JFS

+0

@JFS bạn đã nhận giải pháp chưa? – Irshad

+0

@ Irshad, không, tôi vẫn không thể thực hiện thao tác thả ... – JFS

0

Ngoài những gì GoodSp33d đề cập ở trên, bạn cũng thiếu chức năng ủy nhiệm validate được yêu cầu để chấp nhận giảm. Trong Swift, đây là:

func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation 

Lưu ý giá trị trả về, NSDragOperation. Phương thức này nên chứa mã xác định chính xác loại hoạt động kéo nào đang được thử và trả về giá trị này. Trả lại những điều sai trái có thể dẫn đến một số lỗi khá khó chịu.

Lưu ý thêm rằng để hỗ trợ loại thao tác này, lớp bố cục chế độ xem bộ sưu tập bạn đang sử dụng cũng phải hỗ trợ kéo và thả. Bố trí luồng nên thực hiện việc này, nhưng nếu bạn đang sử dụng bố cục tùy chỉnh, bạn có thể cần điều chỉnh nó để hỗ trợ kéo và thả để chế độ xem bộ sưu tập có thể phát hiện các mục tiêu thả hợp lệ và xác định chỉ mục phù hợp con đường cho họ.