Pregunta

I have NSOutlineView with custom cell named ListCell. I set label and icon to my custom cell. Then NSOutlineView crash on error exc_bad_access code=13. Do you have any idea, how to repair it? Thx for reply.

- (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;
}
¿Fue útil?

Solución 2

Simply I changed NSOutlineView to NSTableView and everything is ok.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top