我实现了一个课程来学习提供NSOutlineView的委托和数据源。
我想在大纲视图中显示的内容是这样的:
| -------第0栏----------------- | ------------第1-栏--------------------- |
- 起居室|客厅描述
+客厅 - 1 |客厅描述 - 1
+客厅 - 2 |客厅描述 - 2
- 卧室|卧室的描述
+卧室 - 1 |卧室描述 - 1
+卧室 - 2 |卧室描述 - 2
+卧室 - 3 |卧室描述 - 3
- 餐厅|餐厅介绍
+表|表格
我实现了delegate和dataSource方法。但是问题是:当第一次显示大纲视图时,它会顺利进行。但是当我扩展其中一个时,只要其子计数大于1,就会出错。 有时候我会得到一个奇怪的大纲视图项目对象,有时我的地址不好。
我很抱歉,我必须在这里发布所有代码。但我找不到哪里出错......
我的问题是:
任何人都可以复制我的代码并构建它吗?我想知道你是否也从我的代码中得到了错误的地址问题。如果是这样,有人可以找出哪里出错了吗?我找不到问题,因此不知道如何解决这个问题。
谢谢!
delegate和dataSource类如下实现(使用ARC,单元格的所有项都设置为NSString对象):
@implementation AMCTreeData
{
NSTableColumn *_titleColumn;
NSTableColumn *_descriptionColumn;
}
/**********/
/* initialize the outline view */
- (void)awakeFromNib
{
/* get column object */
_titleColumn = [[_outlineView tableColumns] objectAtIndex:0];
_descriptionColumn = [[_outlineView tableColumns] objectAtIndex:1];
/* set a new text template */
ImageAndTextCell *newCellTemplate = [[ImageAndTextCell alloc] init];
[newCellTemplate setEditable:NO];
[_titleColumn setDataCell:newCellTemplate];
[_descriptionColumn setDataCell:newCellTemplate];
}
/**********/
/* determine whether a line should be expanded */
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
if (!item)
return YES;
else if ([item isKindOfClass:[NSString class]])
{
if ([(NSString*)item isEqualToString:@"Living Room"])
return YES;
else if ([(NSString*)item isEqualToString:@"Bedrooms"])
return YES;
else if ([(NSString*)item isEqualToString:@"Dining Rooms"])
return YES;
else
return NO;
}
else
return NO;
}
/**********/
/* determine member of each groups */
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
AMCPrintf("==============\n\tCheck child amount for %s", [[item description] UTF8String]);
if (!item)
{
/* top item */
return 3;
}
else if ([item isKindOfClass:[NSString class]])
{
if ([(NSString*)item isEqualToString:@"Living Room"])
return 2;
else if ([(NSString*)item isEqualToString:@"Bedrooms"])
return 3;
else if ([(NSString*)item isEqualToString:@"Dining Rooms"])
return 1;
else
return 0;
}
else
return 0;
}
/**********/
/* determine a cell item's content */
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
if (!item)
{
/* continue */
}
else if (![item isKindOfClass:[NSString class]])
{
/* Unsupported */
AMCDebug(@"Object %@ ??? What the ...", item); /* Yep, it may go here */
return @"";
}
/* First column */
if (tableColumn == _titleColumn)
{
AMCPrintf("Require object value (0) for \"%s\"", [[item description] UTF8String]);
if (!item)
return @"TOP_0";
else
return item;
}
/* second column */
else if (tableColumn == _descriptionColumn)
{
AMCPrintf("Require object value (1) for \"%s\"", [[item description] UTF8String]);
if (!item)
return @"TOP_1";
else
return [NSString stringWithFormat:@"Detailed for %@", item];
}
else
{
AMCPrintf("!!! Unsopported column %s", [[item description] UTF8String]);
return nil;
}
}
/**********/
/* determine whose child a item is */
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
AMCPrintf("Require child for %s in Line %ld", [[item description] UTF8String], index);
if (!item)
{
id ret;
switch (index)
{
case 0:
ret = @"Living Room";
break;
case 1:
ret = @"Bedrooms";
break;
case 2:
ret = @"Dining Rooms";
break;
default:
ret = nil;
break;
}
return ret;
}
else if ([item isKindOfClass:[NSString class]])
{
if ([(NSString*)item isEqualToString:@"Living Room"])
return [NSString stringWithFormat:@"Living Room - %ld", index + 1];
else if ([(NSString*)item isEqualToString:@"Bedrooms"])
return [NSString stringWithFormat:@"Room - %ld", index + 1];
else if ([(NSString*)item isEqualToString:@"Dining Rooms"])
return @"Table";
else
return nil;
}
else
return nil;
}