NSOutlineView拖放(项目旁边)不适用于复杂项目

时间:2018-07-11 17:03:40

标签: objective-c xcode drag-and-drop nsoutlineview

我的基本示例非常适合标准ObjC类表示的项目。 请参见以下示例中的self.list的初始化:

- (void) updateViews;
{
    NSDictionary *firstParent = [NSDictionary dictionaryWithObjectsAndKeys:@"Foo",@"parent",[NSArray arrayWithObjects:@"Foox",@"Fooz", nil],@"children", nil];
    NSDictionary *secondParent = [NSDictionary dictionaryWithObjectsAndKeys:@"Bar",@"parent",[NSArray arrayWithObjects:@"Barx",@"Barz", nil],@"children", nil];
    self.list = [NSArray arrayWithObjects:firstParent,secondParent, nil];

    self.outlineView.delegate = self;
    self.outlineView.dataSource = self;


    // Enable Drag and Drop
    [self.outlineView registerForDraggedTypes:@[LOCAL_REORDER_PASTEBOARD_TYPE]];
}


#pragma mark - NSOutlineViewDelegate

- (BOOL) outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item
{
    if ([item isKindOfClass:[NSDictionary class]]) {
        return YES;
    }
    else {
        return NO;
    }
}

- (NSInteger) outlineView:(NSOutlineView*)outlineView numberOfChildrenOfItem:(id)item
{
    if (item == nil) { //item is nil when the outline view wants to inquire for root level items
        return [self.list count];
    }

    if ([item isKindOfClass:[NSDictionary class]]) {
        return [[item objectForKey:@"children"] count];
    }

    return 0;
}

- (id) outlineView:(NSOutlineView*)outlineView child:(NSInteger)index ofItem:(id)item
{
    if (item == nil) { //item is nil when the outline view wants to inquire for root level items
        return [self.list objectAtIndex:index];
    }

    if ([item isKindOfClass:[NSDictionary class]]) {
        return [[item objectForKey:@"children"] objectAtIndex:index];
    }

    return nil;
}

- (NSView*) outlineView:(NSOutlineView*)outlineView viewForTableColumn:(NSTableColumn*)tableColumn item:(id)item
{
    NSTableCellView *result = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];

    NSString *txt = nil;
    if ([item isKindOfClass:[NSDictionary class]]) {
        txt = [item objectForKey:@"parent"];//[NSString stringWithFormat:@"%i kids", [[item objectForKey:@"children"] count]];
    }
    else {
        txt = str(item);
    }

    result.textField.stringValue = txt;
    return result;
}

- (BOOL) outlineView:(NSOutlineView*)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard 
{
    [pasteboard declareTypes:[NSArray arrayWithObject:LOCAL_REORDER_PASTEBOARD_TYPE] owner:self];
    [pasteboard setData:[@"just a test - not yet implemented" dataUsingEncoding:NSUTF8StringEncoding] forType:LOCAL_REORDER_PASTEBOARD_TYPE];
    return YES;
}

- (NSDragOperation) outlineView:(NSOutlineView*)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index 
{
    NSUInteger op = NSDragOperationNone;
    if (index != NSOutlineViewDropOnItemIndex){
        op = NSDragOperationMove;
    }
    return op;
}

按预期进行拖放操作。

enter image description here

一旦我修改了child:(NSInteger)index ofItem:(id)item方法以返回我自己类的复杂项,特别是TreeItem,那么拖放操作仅允许:

  1. 放置物品

enter image description here

  1. 拖放到父项旁边

enter image description here

但是,如第一张图片所示,在叶子项旁边的放置是不可能的。有什么线索吗?

让我还添加不起作用的复杂项目的示例代码。 两种情况下的Storyboard和dragDrop代码都是相同的。

#define LOCAL_REORDER_PASTEBOARD_TYPE @"LOCAL_REORDER_PASTEBOARD_TYPE"
#define str(...) [@[__VA_ARGS__] componentsJoinedByString:@""]

@interface TreeItem : NSObject
@property (nonatomic) NSArray<TreeItem*> *children;
@property (nonatomic) NSString *name;
+ (nonnull instancetype) itemWithName:(nonnull NSString*)name;
@end

@implementation TreeItem
+ (nonnull instancetype) itemWithName:(nonnull NSString*)name;
{
    TreeItem *obj = [[self alloc] init];
    obj.name = name;
    return obj;
}
- (NSArray<TreeItem*>*) children;
{
    return self.name.length > 3 ? nil : @[
        [TreeItem itemWithName:str(self.name, @"x")],
        [TreeItem itemWithName:str(self.name, @"z")],
    ];
}
@end

@interface SampleVC ()
@property (weak) IBOutlet NSOutlineView *outlineView;
@property (strong) NSArray<TreeItem*> *treeList;
@end

@implementation SampleVC

- (void) updateViews;
{
    self.treeList = @[
        [TreeItem itemWithName:@"Foo"],
        [TreeItem itemWithName:@"Bar"],
    ];
    self.outlineView.delegate = self;
    self.outlineView.dataSource = self;
    [self.outlineView reloadData];
    [self.outlineView registerForDraggedTypes:@[LOCAL_REORDER_PASTEBOARD_TYPE]];
}

- (BOOL) outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item
{
    return item == nil ? YES : ((TreeItem*)item).children != nil;
}

- (NSInteger) outlineView:(NSOutlineView*)outlineView numberOfChildrenOfItem:(id)item
{
    return item == nil ? self.treeList.count : (((TreeItem*)item).children != nil ? ((TreeItem*)item).children.count : 0);
}

- (id) outlineView:(NSOutlineView*)outlineView child:(NSInteger)index ofItem:(id)item
{
    return item == nil ? [self.treeList objectAtIndex:index] : ((TreeItem*)item).children[index];
}

- (NSView*) outlineView:(NSOutlineView*)outlineView viewForTableColumn:(NSTableColumn*)tableColumn item:(id)item
{
    NSTableCellView *result = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
    result.textField.stringValue = ((TreeItem*)item).name;
    return result;
}
@end

1 个答案:

答案 0 :(得分:1)

大纲视图找不到子行,并在视图顶部绘制了放置指示器。如果每次调用NSOutlineViewrowForItem:返回一个新的新TreeItem数组,则TreeItem的{​​{1}}无法工作。