【问题标题】:Dynamic NSOutlineView data source动态 NSOutlineView 数据源
【发布时间】:2013-05-27 16:13:46
【问题描述】:

所以我实现了一个PXSourceList 数据源,它几乎是Apple 的NSOutlineView 数据源示例的副本。

事情是这样的……

- (NSUInteger)sourceList:(PXSourceList*)sourceList numberOfChildrenOfItem:(id)item; {
    if (item == nil) {
        // item is nil so it's part of the very top hierarchy.
        // return how many sections we need.
        return 2;
    }
    else {
        if ([item class] == [TSFileSystemItem class] ) {
            return [item numberOfChildren];
            // if item isn't nil and it's a TSFileSystemItem, then return it's children.
        }
        if ([item class] == [TSWorkspaceItem class]) {
            return 2; // i don't know, random items.
        }
        else {
            NSLog(@"this is a special object.");
        }
    }
}

- (BOOL)sourceList:(PXSourceList *)aSourceList isItemExpandable:(id)item {
    if (item == nil) {
        return YES;
    }
    else {
        // if the number of children of the item is -1
        BOOL gibberhook = ([item numberOfChildren] != -1);
        return gibberhook;
    }
}

-(id)sourceList:(PXSourceList *)aSourceList child:(NSUInteger)index ofItem:(id)item {
    if (item == nil) {
        return [TSFileSystemItem rootItem];
    }
    else {
        return [(TSFileSystemItem *)item childAtIndex:index];
    }
}

- (id)sourceList:(PXSourceList *)aSourceList objectValueForItem:(id)item {
    if (item == nil) {
        return @"/";
    } else {
        if (item == [TSFileSystemItem rootItem]) {
            return PROJECT_FILES;
        }
        else {
            return [item relativePath];
        }
    }
}

神秘的TSFileSystemItem来自这里:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/OutlineView/Articles/UsingOutlineDataSource.html

除了我想将源列表划分为多个部分(根单元格)之外,所有这些都可以。一个显示文件层次结构(检查),另一个...

另一个将包含一个NSMutableArray,我从其他部分添加项目。听起来很复杂?更好的解释。单击具有文件层次结构的部分中的项目,并将其添加到其他部分。

我已经尝试在 Apple 文档的帮助下解决这个问题,但我仍然找不到一种简单、高效、稳定的方法来使用我上面提到的功能制作 2 个部分。要是像为UITableView 配置数据源一样简单就好了……

谁能帮帮我?

【问题讨论】:

  • 您注意到NSOutlineViewDelegate 中的- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item了吗?
  • 我会调查的,谢谢。

标签: objective-c cocoa datasource nsoutlineview pxsourcelist


【解决方案1】:

当调用 childrenForItem 委托方法并且 item 为 nil 时,这会请求树的根。如果你返回和数组,那么树将为该数组中的每个元素都有一个根节点。

- (NSArray *)childrenForItem:(id)item {
    if (item == nil) {
        return [self.rootTreeNode childNodes];
    } else {
        return [item childNodes];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多