hor*_*guy 11 cocoa objective-c nsoutlineview
出于学习目的,我想将基于单元格的NSOutlineView转换为基于视图的NSOutlineView,
基本上我想要以下内容:
NSTreeController
这是'世界上最简单的NSOutlineView'示例http://www.cocoasteam.com/Cocoa_Steam/Worlds_Simplest_Demo.html
我想知道是否有人可以修改它以使其基于视图并像我上面说的那样工作:) :)
我试过看苹果的例子,在互联网上的其他地方搜索,但我仍然无法让它工作 - 所以非常感谢提前:)
Dr.*_*eon 10
好的,所以你想要一个NSOutlineView
带ImageAndTextCell
细胞,对吧?
让我们做一个这种最典型的例子:一个简单的文件浏览器.
我们需要什么:
NSOutlineView
(给你的AppDelegate概述,如fileOutlineView
)NameColumn
SizeColumn
ModifiedColumn
现在,至于其余部分,我将以编程方式完成所有操作,以便您了解正在发生的事情......
如何设置(例如- (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];
Run Code Online (Sandbox Code Playgroud)
连接点:
/*******************************************************
*
* 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];
}
}
}
Run Code Online (Sandbox Code Playgroud)
提示:课程可以在这里找到.您还会注意到我正在使用的其他一些方法,显然Cocoa不支持这些方法(例如,或者),但是自己实现它们并不困难......) ImageAndTextCell
isFileHidden
isFolder
folderContentsWithPathAndBackIgnoringHidden
我已经创建了一个小样本项目.
查看github上的besi/mac-quickies.大多数东西要么在IB中完成,要么可以在AppDelegate中找到
归档时间: |
|
查看次数: |
11060 次 |
最近记录: |