jaz*_*man 6 objective-c nsoutlineview nsmutablearray
我正在尝试为列出的对象创建一个带有自定义标头组(父节点)的NSOutlineVew.(注意:我有基于单元格的NSOutlineView).例如,它看起来像Xcode"Navigator"或Numbers侧边栏.我使用每个类别的分隔属性的默认组,但它看起来不像我想要的那样.我需要一个父节点(单元格),我可以直观地调整(添加一个控件元素和图像).
我尝试通过将一组对象传递给NSDictionary来完成此操作,为每个组提供特定的特定键.结果,通过NSLog一切都正确显示,但是这个变量作为程序NSOulineView的源传输失败.
ProjectViewController.h
@interface ProjectViewController : NSViewController <NSOutlineViewDataSource, NSObject> {
IBOutlet NSOutlineView *outlineView;
FSEntity *content;
}
@property (readonly, assign) NSMutableArray *objects;
@end
Run Code Online (Sandbox Code Playgroud)
ProjectViewController.m
@implementation ProjectViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
// Setting default path to the local file or directory
NSString *home = NSHomeDirectory();
NSURL *url = [[NSURL alloc] initFileURLWithPath:home];
content = [[FSEntity alloc] initWithURL:url];
[self defineContentNSOutlineView];
NSLog(@"Array: %@",_objects);
// Basic ?onfiguration an instance NSOutlineView
[self configurationNSOutlineView];
} return self;
}
@synthesize objects = _objects;
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return (item == nil) ? [content.children objectAtIndex:index] : [((FSEntity *)item).children objectAtIndex:index];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return (item == nil) ? content.children.count > 0 : ((FSEntity *)item).children.count > 0;
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return (item == nil) ? content.children.count : ((FSEntity *)item).children.count;
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if ([item isKindOfClass:[FSEntity class]]) {
return [((FSEntity *)item) title];
}
return nil;
}
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
if ([cell isKindOfClass:[ImageAndTextCell class]]) {
ImageAndTextCell *textField = (ImageAndTextCell *)cell;
[textField setImage:[item icon]];
}
}
- (void)defineContentNSOutlineView {
NSMutableArray *objects = [NSMutableArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"FINDER", @"title", [NSArray arrayWithObjects:[NSDictionary dictionaryWithObject:content.children forKey:@"title"], nil], @"children",[NSNumber numberWithBool:YES], @"header", nil], nil];
_objects = objects;
}
- (void)configurationNSOutlineView {
[outlineView sizeLastColumnToFit];
[outlineView setFloatsGroupRows:NO];
[outlineView reloadData];
[outlineView expandItem:nil expandChildren:YES];
}
@end
Run Code Online (Sandbox Code Playgroud)
为了更容易想象它的样子,我在计划中展示了它:
+--------------------------------------------+
| ? FINDER FILES ? ? |
| 03143553.file |
| ? Desktop |
| ? Documents |
| ? Downloads |
| ? Movies |
| ? Music |
| ? Pictures |
+--------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
我现在拥有的(NSOulineView,不使用NSTreeController);
+--------------------------------------------+
| 03143553.file |
| ? Desktop |
| ? Documents |
| ? Downloads |
| ? Movies |
| ? Music |
| ? Pictures |
+--------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
我知道Apple的"SourceView"示例,但我不知道如何添加到创建的组,对象数组(文件和文件夹),NSTreeContoller只显示层次结构的第一个元素(不包括):
+--------------------------------------------+
| ? FINDER FILES |
| 03143553.file |
| Desktop |
| Documents |
| Downloads |
| Movies |
| Music |
| Pictures |
+--------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
SourceView示例的修改方法:
- (void)addFinderSection {
[self addFolder:@"FINDER FILES"];
NSError *error = nil;
NSEnumerator *urls = [[[NSFileManager defaultManager] contentsOfDirectoryAtURL:self.url includingPropertiesForKeys:[NSArray arrayWithObjects: nil] options:(NSDirectoryEnumerationSkipsHiddenFiles) error:&error] objectEnumerator];
for (NSURL *url in urls) {
BOOL isDirectory;
if ([[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory]) {
if (isDirectory) {
[self addChild:[url path] withName:NO selectParent:YES];
} else {
[self addChild:[url path] withName:NO selectParent:YES];
}
}
}
[self selectParentFromSelection];
}
Run Code Online (Sandbox Code Playgroud)
此方法仅显示第一个对象,如后一个方案所示.
还有一个问题,正如我之前所说,如何将节点**"FINDER FILES"**按钮添加到单元格的右侧.
你能帮帮我吗?我知道,也许不是那么难,但我刚开始学习Objective-C,我不知道该怎么做.谢谢.
根据您提供的起始代码,我能够使用基于单元格的 NSOutlineViews 来完成一些工作。您发布的代码似乎不完整,因此很难知道您到底想从丢失的 FSEntity 类中获取什么。我只使用了基础类:NSArray 用于节点列表(即根节点和子节点),NSDictionaries 用于每个节点本身(根节点和子节点),NSURL 作为文件系统引用。我尝试尽可能接近您的原始代码。最后,它看起来像这样:
\n\n项目视图控制器.h
\n\n@interface ProjectViewController : NSViewController <NSOutlineViewDataSource, NSObject>\n{\n IBOutlet NSOutlineView *outlineView;\n NSURL *content;\n}\n\n@end\nRun Code Online (Sandbox Code Playgroud)\n\n项目视图控制器.m
\n\n#import "ProjectViewController.h"\n\n@interface ProjectViewController () {\n NSMutableArray* _objects;\n}\n@property (nonatomic, retain, readwrite) NSMutableArray* objects;\n@end\n\n@implementation ProjectViewController\n\n@synthesize objects = _objects;\n\n- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {\n if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {\n // Initialization code here.\n // Setting default path to the local file or directory\n NSString *home = NSHomeDirectory();\n content = [[NSURL alloc] initFileURLWithPath:home];\n\n [self defineContentNSOutlineView];\n NSLog(@"Array: %@",_objects);\n\n // Basic \xd1\x81onfiguration an instance NSOutlineView\n [self configurationNSOutlineView];\n }\n return self;\n}\n\n// nodes have 3 keys: title, url, icon\n- (NSArray*)p_childrenForNode: (NSMutableDictionary*)node {\n if (nil == node)\n return self.objects;\n\n NSArray* retVal = nil;\n if (nil == (retVal = [node valueForKey: @"children"]))\n {\n NSMutableArray* children = [NSMutableArray array];\n for (NSURL* urlInDir in [[NSFileManager defaultManager] contentsOfDirectoryAtURL: [node objectForKey: @"url"]\n includingPropertiesForKeys: [NSArray arrayWithObjects: NSURLNameKey, NSURLEffectiveIconKey, nil]\n options: 0\n error: NULL])\n {\n id name = [urlInDir getResourceValue: &name forKey: NSURLNameKey error: NULL] ? name : @"<Couldn\'t get name>";\n id icon = [urlInDir getResourceValue: &icon forKey: NSURLEffectiveIconKey error: NULL] ? icon : nil;\n\n NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: urlInDir, @"url", name, @"title", nil];\n\n if (icon)\n [dict setObject: icon forKey: @"icon"];\n\n [children addObject: dict];\n }\n\n retVal = children;\n\n if (children)\n [node setValue: children forKey: @"children"];\n }\n return retVal;\n}\n\n- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {\n NSMutableDictionary* itemDict = (NSMutableDictionary*)item;\n NSArray* children = [self p_childrenForNode: itemDict];\n return children.count > index ? [[[children objectAtIndex: index] retain] autorelease] : nil;\n}\n\n- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {\n NSMutableDictionary* itemDict = (NSMutableDictionary*)item;\n NSArray* children = [self p_childrenForNode: itemDict];\n return children.count > 0;\n}\n\n- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {\n NSMutableDictionary* itemDict = (NSMutableDictionary*)item;\n NSArray* children = [self p_childrenForNode: itemDict];\n NSInteger retVal = children.count;\n return retVal;\n}\n\n- (id)outlineView:(NSOutlineView *)pOutlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {\n\n NSImage* icon = [item objectForKey: @"icon"];\n NSString* title = [item objectForKey: @"title"];\n id value = nil;\n\n if (icon) {\n [icon setSize: NSMakeSize(pOutlineView.rowHeight - 2, pOutlineView.rowHeight - 2)];\n NSTextAttachment* attachment = [[[NSTextAttachment alloc] init] autorelease];\n [(NSCell *)[attachment attachmentCell] setImage: icon];\n NSMutableAttributedString *aString = [[[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy] autorelease];\n [[aString mutableString] appendFormat: @" %@", title];\n value = aString;\n } else {\n value = title;\n }\n\n return value;\n}\n\n- (void)defineContentNSOutlineView {\n // Make root object\n NSMutableDictionary* rootObj = [NSMutableDictionary dictionaryWithObjectsAndKeys:\n @"FINDER", @"title",\n content, @"url",\n nil];\n\n id icon = [content getResourceValue: &icon forKey: NSURLEffectiveIconKey error: NULL] ? icon : nil;\n if (icon)\n [rootObj setObject: icon forKey: @"icon"];\n\n // Set it\n self.objects = [NSMutableArray arrayWithObject: rootObj];\n}\n\n- (void)configurationNSOutlineView {\n [outlineView sizeLastColumnToFit];\n [outlineView setFloatsGroupRows:NO];\n [outlineView reloadData];\n [outlineView expandItem:nil expandChildren:YES];\n}\n\n@end\nRun Code Online (Sandbox Code Playgroud)\n\n我将整个工作示例项目发布到GitHub。
\n| 归档时间: |
|
| 查看次数: |
428 次 |
| 最近记录: |