0

我有 NSOutlineView 和名为 ListCell 的自定义单元格。我将标签和图标设置为我的自定义单元格。然后 NSOutlineView 在错误 exc_bad_access code=13 上崩溃。你有什么想法,如何修复它?谢谢回复。

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
  return [[[DataSingleton sharedData] pages] count];
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
  return NO;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
  return item;
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
  ListTableCellView *cell = [outlineView makeViewWithIdentifier:@"ListCell" owner:self];

  cell.label.stringValue = [NSString stringWithFormat:@"%ld", index + 1];
  [cell.label setBackgroundColor:[NSColor clearColor]];

  if ([self.icons objectForKey:[NSString stringWithFormat:@"%ld", index]])
    [[cell icon] setImage:[self.icons objectForKey:[NSString stringWithFormat:@"%ld", index]]];

  return cell;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item {
 return YES;
}
4

2 回答 2

1

These methods are the data source delegate methods. As such they should be concerned only with handling the data. What you are doing here makes no sense to me.

The view will expect back objects that it can use in its table cell's setStringValue: etc methods. Giving back a custom cell in -outlineView:objectValueForTableColumn:byItem: is probably going to confuse it.

Also, I don't think you are handling the counts properly. -outlineView:numberOfChildrenOfItem: should return the count of sub items to the item passed in. The index passed in -outlineView:child:ofItem: can be anywhere between 0 and that count - 1.

Furthermore, I think you need to ensure that the items you return are consistent between invocations of -reloadData. So, if, say, the view asks for child 5 of item nil (nil means the "root level" item) you should consistently return the same object each time. I haven't seen this documented anywhere, but I had a similar problem once where I was generating the objects on the fly.

于 2013-05-08T15:57:16.850 回答
-4

只是我将 NSOutlineView 更改为 NSTableView ,一切正常。

于 2013-05-14T17:46:45.043 回答