测试时,我发现了outlineView:objectValueFortableColumn:byitem:在填充最后一个可见行之后被称为一个额外时间,导致exc_bad_access错误。
因此,如果我的显示器显示10行,则第9行填充后的对象ValueFortableColumn再次调用(无需大写:Child:ofitem:ofitem:ofitem:andlineView:isitempardable:isitemparbandable:被称为)。额外的呼叫总是发生在最后一个可见行被填充后。
这是我的大概代码。我的测试数据集中有2列和114个记录。
// (1)
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if (!item) {
NSInteger parentCount = [[Parent countParents:self.dataSetID usingManagedObjectContext:self.context] integerValue];
return parentCount;
}
Parent *thisParent = item;
NSInteger childCount = [[thisParent.child allObjects] count];
return childCount;
}
// (2)
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
NSArray *parentArray = [Parent parentData:self.dataSetID usingManagedObjectContext:self.context];
Parent *thisParent = [parentArray objectAtIndex:index];
if (!item) {
return thisParent;
}
NSArray *children = [NSArray arrayWithObject:[thisParent.child allObjects]];
Child *thisChild = [children objectAtIndex:index];
return thisChild;
}
// (3)
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
if ([item isKindOfClass:[Parent class]]) {
Parent *thisParent = item;
if ([[thisParent.child allObjects] count] > 0) {
return YES;
}
}
return NO;
}
// (4)
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if (!item) {
return nil;
}
if ([tableColumn.identifier isEqualToString:@"column1"]) {
if ([item isKindOfClass:[Parent class]]) {
Parent *thisParent = item;
return thisParent.name;
}
Child *thisChild = item;
return thisChild.name;
}
// This is column2
if ([item isKindOfClass:[Parent class]]) {
Parent *thisParent = item;
return thisParent.age;
}
Child *thisChild = item;
return thisChild.age;
}
我注意到这些方法是按顺序调用的:1,2,3,4,4,2,3,4,4 ...以填充两列NSoutlineView。对于最后一个可见的行,该顺序为:2,3,4,4,4,最后一次呼叫方法#4(outlineView:objectValuefortableColumn:byitem:byitem :)导致例外。
我无法告诉您要传递给该方法的值,因为它会在呼叫上破裂。即使该方法中的第一件事是日志语句,也不会执行。
所以我很难过。有什么想法,为什么这会破裂?我是否对实现?
在 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
中您似乎没有使用item
-您应该返回child item at the specified index of a given item
。
顺便说一句,您的代码效率低下 - 您应该尝试尽可能快地运行这些运行。您可能想要: -
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
if (item == nil)
return [[Parent parentData:self.dataSetID usingManagedObjectContext:self.context] objectAtIndex:index];
return [[NSArray arrayWithObject:[item.child allObjects]] objectAtIndex:index];
}