我如何做一个简单的基于视图的NSOutlineView? [英] How do i make a simple view-based NSOutlineView?

查看:293
本文介绍了我如何做一个简单的基于视图的NSOutlineView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了学习目的,我想将一个基于单元格的NSOutlineView转换为基于视图的



基本上我想要以下:




  • 而不是正常单元格,我想要一个图像和文本表格单元格视图

  • 是的股票NSApplicationIcon和文本可以只是'hello世界':)

  • 我想这样做而不使用绑定和 NSTreeController



以下是'worlds simplest NSOutlineView'示例http://www.cocoasteam.com/Cocoa_Steam/Worlds_Simplest_Demo.html



我想知道是否有人可以修改它基于视图和像我上面所说的工作:))



我试过看苹果的例子,在互联网上的其他地方搜索,但我还是不能得到它工作 - 所以非常感谢提前:)

解决方案

确定,所以你想要一个 NSOutlineView c>使用 ImageAndTextCell 单元格,对吗?



让我们做一个最典型的例子


$ b

    >
  • NSOutlineView (将大纲写入您的AppDelegate,如 fileOutlineView

  • 使用以下标识符(在Interface Builder中设置)在Outline中创建3列: NameColumn SizeColumn ModifiedColumn



,我会以编程方式做,所以你可以很好地了解发生了什么...



如何设置它 - (void)awakeFromNib ):

  /设置数据源和代理
[fileOutlineView setDataSource:(id< NSOutlineViewDataSource>)self];
[fileOutlineView setDelegate:(id< NSOutlineViewDelegate>)self];

//将第一列的单元格设置为`ImageAndTextCell`s
ImageAndTextCell * iatc = [[ImageAndTextCell alloc] init];
[iatc setEditable:NO];
[[[FileOutlineView tableColumns] objectAtIndex:0] setDataCell:iatc];

连接点

  / ********************************* ******************** 
*
* OUTLINE-VIEW DATASOURCE
*
******* ************************************************ /

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

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if(item == nil)
{
// Root
return [[filePath folderContentsWithPathAndBackIgnoringHidden] count];
}
else
{
if([item isFolder])
{
return [[item folderContentsWithPathAndBackIgnoringHidden] count];
}
else
{
return 0;
}
}
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)indexItem:(id)item
{
if(item == nil)
{
// Root
return [[filePath folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
}

if([item isFolder])
{
return [[item folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
}

//文件
return nil;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item
{
if列表标识符] isEqualToString:@NameColumn])
{
return [item lastPathComponent];
}
else if([[theColumn identifier] isEqualToString:@SizeColumn])
{
if([item isFolder])return @ - ;
else return [NSString stringWithFormat:@%d,[item getFileSize]];
}
else if([[theColumn identifier] isEqualToString:@ModifiedColumn])
{
if([item isFolder])return @;
else return [NSString stringWithFormat:@%@,[item getDateModified]];
}

//永远不会到达这里
return nil;
}

/ ************************************ *******************
*
* OUTLINE-VIEW DELEGATE
*
******** *********************************************** /

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
return YES;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
{
return NO;
}

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
[cell setDrawsBackground:NO];

if([item isFileHidden])[cell setTextColor:[NSColor grayColor]];
else [cell setTextColor:[NSColor whiteColor]];

if([[tableColumn identifier] isEqualToString:@NameColumn])
{
if([item isFolder])
[cell setImage:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] size:15.0];
else
[cell setImage:[[NSWorkspace sharedWorkspace] iconForFile:item] size:15.0];

if([item isFileHidden])
{
[cell setFileHidden:YES];
}
else
{
[cell setFileHidden:NO];
}

}

}



< hr>

提示: ImageAndTextCell //developer.apple.com/library/mac/#samplecode/SourceView/Listings/ImageAndTextCell_m.html\">在这里。你还会注意到我使用的一些其他方法,显然不支持Cocoa(例如 isFileHidden isFolder folderContentsWithPathAndBackIgnoringHidden ),但是自己实现并不难。)


For learning purposes i would like to convert a cell-based NSOutlineView to a view-based one,

basically i would like the following:

  • instead of a normal cell, i'd like an 'image and text table cell view'
  • the image can be the stock NSApplicationIcon and the text can just be 'hello world' :)
  • I'd like to do this without using bindings and NSTreeController

Here is the 'worlds simplest NSOutlineView' example http://www.cocoasteam.com/Cocoa_Steam/Worlds_Simplest_Demo.html

I wonder if someone could modify it to make it view-based and work like i said above :) :)

I've tried looking at apple examples, and searching elsewhere on the internet but i still can't get it to work - so thanks very much in advance :)

解决方案

OK, so you want an NSOutlineView with ImageAndTextCell cells, right?

Let's do one of the most typical examples of this kind : a simple file explorer.

What we'll need :

  • an NSOutlineView (put an outline to your AppDelegate, as fileOutlineView)
  • create 3 columns in the Outline with the following Identifiers (set them up in Interface Builder) : NameColumn, SizeColumn, ModifiedColumn

Now, as for the rest, I'll do it all programmatically, so that you get a good idea of what's going on...

How to set it up (e.g. in - (void)awakeFromNib):

// set the Data Source and Delegate
[fileOutlineView setDataSource:(id<NSOutlineViewDataSource>)self];
[fileOutlineView setDelegate:(id<NSOutlineViewDelegate>)self];

// set the first column's cells as `ImageAndTextCell`s
ImageAndTextCell* iatc = [[ImageAndTextCell alloc] init];
[iatc setEditable:NO];
[[[fileOutlineView tableColumns] objectAtIndex:0] setDataCell:iatc];

Connecting the dots :

/*******************************************************
 *
 * OUTLINE-VIEW DATASOURCE
 *
 *******************************************************/

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

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{    
    if (item==nil)
    {
        // Root
        return [[filePath folderContentsWithPathAndBackIgnoringHidden] count];
    }
    else
    {        
        if ([item isFolder])
        {
            return [[item folderContentsWithPathAndBackIgnoringHidden] count];
        }
        else
        {
            return 0;
        }
    }
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    if (item == nil)
    { 
        // Root
        return [[filePath folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
    }

    if ([item isFolder])
    {
        return [[item folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
    }

    // File
    return nil;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item
{          
    if ([[theColumn identifier] isEqualToString:@"NameColumn"])
    {
        return [item lastPathComponent];
    }
    else if ([[theColumn identifier] isEqualToString:@"SizeColumn"])
    {
        if ([item isFolder]) return @"--";
        else return [NSString stringWithFormat:@"%d",[item getFileSize]];
    }
    else if ([[theColumn identifier] isEqualToString:@"ModifiedColumn"])
    {
        if ([item isFolder]) return @"";
        else return [NSString stringWithFormat:@"%@",[item getDateModified]];
    }

    // Never reaches here
    return nil;
}

/*******************************************************
 *
 * OUTLINE-VIEW DELEGATE
 *
 *******************************************************/

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
    return YES;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
{
    return NO;
}

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    [cell setDrawsBackground:NO];

    if ([item isFileHidden]) [cell setTextColor:[NSColor grayColor]];
    else [cell setTextColor:[NSColor whiteColor]];

    if ([[tableColumn identifier] isEqualToString:@"NameColumn"])
    {
        if ([item isFolder])
            [cell setImage:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] size:15.0];
        else
            [cell setImage:[[NSWorkspace sharedWorkspace] iconForFile:item] size:15.0];

        if ([item isFileHidden])
        {
            [cell setFileHidden:YES];
        }
        else
        {
            [cell setFileHidden:NO];
        }

    }

}


Hint : ImageAndTextCell class can be found here. You'll also notice a few other methods I'm using, which are obviously NOT supported by Cocoa (e.g. isFileHidden, isFolder or folderContentsWithPathAndBackIgnoringHidden) but it's not that difficult to implement them yourself...)

这篇关于我如何做一个简单的基于视图的NSOutlineView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆