Objective c NSOutlineView'中的地址不正确;s委托和数据源
我实现了一个类来研究提供NSOutlineView的委托和数据源 我想在大纲视图中显示的内容如下: |------第0列--------------------第1列--------------------第1列-----------------Objective c NSOutlineView'中的地址不正确;s委托和数据源,objective-c,macos,cocoa,exc-bad-access,nsoutlineview,Objective C,Macos,Cocoa,Exc Bad Access,Nsoutlineview,我实现了一个类来研究提供NSOutlineView的委托和数据源 我想在大纲视图中显示的内容如下: |------第0列--------------------第1列--------------------第1列----------------- -客厅 | 客厅说明 + 客厅-1 | 客厅说明-1 + 客厅-2 | 客厅说明-2 -卧室 | 卧室说明 + 卧室-1 | 卧室说明-1 + 卧室-2 | 卧室说明-2 + 卧室-3 | 卧室说明-3 -餐厅
-客厅 | 客厅说明
+ 客厅-1 | 客厅说明-1
+ 客厅-2 | 客厅说明-2
-卧室 | 卧室说明
+ 卧室-1 | 卧室说明-1
+ 卧室-2 | 卧室说明-2
+ 卧室-3 | 卧室说明-3
-餐厅 | 餐厅说明
+ 桌子 | 表的说明
我实现了委托和数据源方法。但是问题是:当第一次显示大纲视图时,它运行良好。但当我扩展其中一个时,只要其子计数大于1,就会出现问题有时我得到一个奇怪的大纲视图项目对象,有时我得到一个错误的地址。 非常抱歉,我必须在这里发布我的所有代码。但我找不到哪里出错了… 我的问题是:
有人能复制我的代码并构建它吗?我想知道你是否也从我的代码中得到了地址错误的问题。如果是这样,谁能找出哪里出了问题?我找不到问题所在,因此不知道如何解决 谢谢大家!
委托和数据源类的实现如下(使用ARC,单元格的所有项都设置为NSString对象):
那你的问题是什么?@user1118321当上面编码了数据源的outline视图展开时,发生了异常:地址错误0x0@user1118321在这里发布代码可能是愚蠢的。。。很抱歉我会编写另一个数据源类,然后再试一次。这一点都不愚蠢。你绝对应该发布你的代码。但你还没问一个问题。你的问题是为什么会发生地址错误?您的问题是如何调试它?你需要什么帮助?@user1118321啊,我明白了。我修改了我的问题,现在更清楚了吗?事实上,我对“坏地址”的例外情况相当恐慌。因为我无法找到应用程序在哪一步出错,即使使用“逐步”也不行,因为在编写代码时没有提出异常。因此,我寻求帮助的是找出错误并修复它。
@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;
}