【问题标题】:Reordering NSCollectionView with (Flow Layout)使用(流布局)重新排序 NSCollectionView
【发布时间】: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


【解决方案1】:

好的!!!因此,您正在重新订购流程布局!这可能很棘手 - 我不在我的 Mac 上,所以我会给你“概述”

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

确保连接您的数据源并委托给视图。 (我没有在代码中看到它,所以我想你是在界面生成器中完成的)

最后,您可能需要检查您的收藏视图是否为强(如此强) - 因为它可能会在显示之前丢失其引用。 - 如果这些都不起作用,请告诉我。

【讨论】:

  • 感谢您的回答。是的,我在界面生成器中连接了数据源并委托给我的视图控制器。也许我不需要流布局。使用内容数组作为重新排序的布局是否更容易?但我只收到 canDragItemsAtIndex 委托调用。
  • Matt - 你有 Delegate 协议集吗?视图控制器:NSCollectionViewDelegate
  • 是的,我确实设置了委托协议。
猜你喜欢
  • 1970-01-01
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 2018-01-25
  • 2012-08-09
  • 2011-05-10
相关资源
最近更新 更多