首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用NSOutlineViewDataSource实现MonoMac

用NSOutlineViewDataSource实现MonoMac
EN

Stack Overflow用户
提问于 2012-01-12 22:52:21
回答 2查看 756关注 0票数 1

我试图为一个NSOutlineView实现一个数据源。问题是我不知道从outlineView:child:ofItem:返回什么类型的对象。

当前代码如下所示:

代码语言:javascript
复制
[Export("outlineView:child:ofItem:")]
public NSObject childOfItem(NSOutlineView outline, int child, NSObject item)
{
    return new MyItem();
}

使用MyItem:

代码语言:javascript
复制
public class MyItem : NSObject
{}

编辑:使用这段代码的,在返回MyItem后我得到了一个InvalidCastException

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-22 03:12:06

如果您从NSOutlineViewDataSource继承了一个新类型,那么就不应该在您自己的方法上重新导出,它的outlineView:child:ofItem:选择器。相反,您应该做的是重写已经导出此选择器的GetChild方法。

代码语言:javascript
复制
public overrride NSObject GetChild (NSOutlineView outlineView, int childIndex, NSObject ofItem)
{
    return new MyItem ();
}

注意:这可能没有帮助,因为我没有尝试过(我主要是做MonoTouch的事情),但是检查您可能在应用程序中重新定义/导出的其他选择器(看看您是否应该从您继承的基类中重写-ing)。

票数 1
EN

Stack Overflow用户

发布于 2012-01-15 15:20:07

你考虑过使用NSTreeController吗?它帮助您管理大纲视图,而且非常方便。NSTreeController使用一个名为NSTreeNode的类来表示大纲视图中的节点,并且每个NSTreeNode都有一个representedObject方法,允许您访问模型对象。

无论如何,如果不想使用NSTreeControllerNSTreeNode,可以直接返回模型对象。以下是苹果指南中的一些目标C代码示例。

代码语言:javascript
复制
@implementation DataSource
// Data Source methods

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {

    return (item == nil) ? 1 : [item numberOfChildren];
}


- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    return (item == nil) ? YES : ([item numberOfChildren] != -1);
}


- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {

    return (item == nil) ? [FileSystemItem rootItem] : [(FileSystemItem *)item childAtIndex:index];
}


- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
    return (item == nil) ? @"/" : [item relativePath];
}

@end


@interface FileSystemItem : NSObject
{
    NSString *relativePath;
    FileSystemItem *parent;
    NSMutableArray *children;
}

+ (FileSystemItem *)rootItem;
- (NSInteger)numberOfChildren;// Returns -1 for leaf nodes
- (FileSystemItem *)childAtIndex:(NSUInteger)n; // Invalid to call on leaf nodes
- (NSString *)fullPath;
- (NSString *)relativePath;

@end


@implementation FileSystemItem

static FileSystemItem *rootItem = nil;
static NSMutableArray *leafNode = nil;

+ (void)initialize {
    if (self == [FileSystemItem class]) {
        leafNode = [[NSMutableArray alloc] init];
    }
}

- (id)initWithPath:(NSString *)path parent:(FileSystemItem *)parentItem {
    self = [super init];
    if (self) {
       relativePath = [[path lastPathComponent] copy];
       parent = parentItem;
       }
    return self;
}


+ (FileSystemItem *)rootItem {
    if (rootItem == nil) {
        rootItem = [[FileSystemItem alloc] initWithPath:@"/" parent:nil];
    }
    return rootItem;
}


// Creates, caches, and returns the array of children
// Loads children incrementally
- (NSArray *)children {

    if (children == nil) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *fullPath = [self fullPath];
        BOOL isDir, valid;

        valid = [fileManager fileExistsAtPath:fullPath isDirectory:&isDir];

        if (valid && isDir) {
            NSArray *array = [fileManager contentsOfDirectoryAtPath:fullPath error:NULL];

            NSUInteger numChildren, i;

            numChildren = [array count];
            children = [[NSMutableArray alloc] initWithCapacity:numChildren];

            for (i = 0; i < numChildren; i++)
            {
                FileSystemItem *newChild = [[FileSystemItem alloc]
                                   initWithPath:[array objectAtIndex:i] parent:self];
                [children addObject:newChild];
                [newChild release];
            }
        }
        else {
            children = leafNode;
        }
    }
    return children;
}


- (NSString *)relativePath {
    return relativePath;
}


- (NSString *)fullPath {
    // If no parent, return our own relative path
    if (parent == nil) {
        return relativePath;
    }

    // recurse up the hierarchy, prepending each parent’s path
    return [[parent fullPath] stringByAppendingPathComponent:relativePath];
}


- (FileSystemItem *)childAtIndex:(NSUInteger)n {
    return [[self children] objectAtIndex:n];
}


- (NSInteger)numberOfChildren {
    NSArray *tmp = [self children];
    return (tmp == leafNode) ? (-1) : [tmp count];
}


- (void)dealloc {
    if (children != leafNode) {
        [children release];
    }
    [relativePath release];
    [super dealloc];
}

@end

这不是MonoMac,但应该是相同的想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8843553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档