0

I have a window with a popup button that allows the user to select a Core Data entity and a table view that should show the information about the entities. The window controller is set as the datasource and delegate for the table view as well as having the action method for the popup button.

The initial display of the table view works fine with the entity that's default in the popup button. This default (Character) entity has 30 objects in the persistent store. The second (Creator) entity has more.

When Creators is selected from the popup menu, the app gives the following error:

** -[_PFArray objectAtIndex:]: index (30) beyond bounds (30)

Here's the simplified code that seems to produce the crash for the relevant methods:

- (IBAction)listPopupButtonAction:(id)sender {
    [[self nameOnlyTableView] reloadData];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    NSString *popupTitle = [[self listPopupButton] selectedItemTitle];
    if ([popupTitle isEqualToString:@"Characters"]) {
        return 30;
    }
    return 39;
}

- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                   row:(NSInteger)row {
    NSString *popupTitle = [[self listPopupButton] selectedItemTitle];
    NSString *name;
    NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"nameOnly" 
                                                            owner:self];

    if ([popupTitle isEqualToString:@"Characters"]) {
        NSArray *array = [CVCCharacter findAll];
        CVCCharacter *character = [array objectAtIndex:row];
        name = [character name];
    } else {
        name = @"name here";
    }

    [[cellView textField] setStringValue:name];  
    return cellView;
}

Note that the default return value in -numberOfRowsInTableView: is very specific. If I set it to ≤38, no crash, ≥39, crash. Again, there are exactly 30 managed objects in the Character entity.

Also note that the crash does not happen if I manually set the name variable when "Characters" is the popup button title.

My guess is that when the window loads, "Characters" is used for the datasource methods and the data is loaded correctly, but when the popup changes to an entity that returns significantly more than the default entity, there aren't enough rows to use for some reason and this _PFArray is only using existing rows the second time around, so it only has 30 to work with. But that doesn't explain why the crash only happens when character data is dynamically retrieved. Perhaps -numberOfRowsInTableView is being called before the popup button title has been completely set but -tableView:viewForTableColumn:row: is being called after it's set, or vice versa?

So, is it possible to use a single table view to do this? Any suggestions?

2
  • What is _PFArray ? An array of PFObjects? It is not shown in your code. Nov 16, 2013 at 11:14
  • It's not part of my code. As far as I can tell, it's a private subclass of NSArray from Apple, not me.
    – Chuck
    Nov 16, 2013 at 21:07

2 Answers 2

1

You should not full the data you want to display in your table view from tableView:viewForTableColumn:row:, not only is this incredibly inefficient in a method where efficiency is primordial since it's going to be called for every row as the user scrolls, but also it exposes your code to crashes if the model changes while between the reloadData and the time your method is called. Instead you should have your model set up and the data pulled before reloadData is called to have full control of what the table view is supposed to display.

0

Thanks to the good people on #macdev (particularly Psy and boredzo), I solved the problem. It turns out that I shouldn't be using the -tableView:viewForTableColumn:row method to do the search in Core Data. When I moved this search into the -listPopupButtonAction: method stored the array of data in a property there, the reload the data after the array has been set up, the crash disappeared. A theory as to why is that there's some chaching going on that caused the problem.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      _dataArray = [NSArray array];
      NSMutableArray *array = [NSMutableArray array];
      NSArray *characters = [CVCCharacter findAll];
      for (CVCCharacter *character in characters) {
        [array addObject:[character name] ? [character name] : @""];
      }
      [self setDataArray:array];
    }
    return self;
}

- (IBAction)listPopupButtonAction:(id)sender {
  NSString *selectedTitle = [[self listPopupButton] selectedItemTitle];

  if ([[self nameOnlyItems] containsObject:selectedTitle]) {
    if ([selectedTitle isEqualToString:@"Chacaracters"]) {
      NSMutableArray *array = [NSMutableArray array];
      NSArray *characters = [CVCCharacter findAll];
      for (CVCCharacter *character in characters) {
        [array addObject:[character name] ? [character name] : @""];
      }
      [self setDataArray:array];
    } else {
      NSMutableArray *array = [NSMutableArray array];
      for (int i = 0; i < 39; i++) {
        [array addObject:[[NSNumber numberWithInt:i] stringValue]];
      }
      [self setDataArray:array];
    }
    [[self nameOnlyTableView] reloadData];

  }
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
  NSString *popupTitle = [[self listPopupButton] selectedItemTitle];
  if ([popupTitle isEqualToString:@"Characters"]) {
    return 30;
  }
  return 39;
}

- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                  row:(NSInteger)row {
  NSString *name= [[self dataArray] objectAtIndex:row];
  NSTableCellView *cellView = cellView = [tableView makeViewWithIdentifier:@"nameOnly"
                                                                     owner:self];
  [[cellView textField] setStringValue:name];
  return cellView;
}
2
  • You shouldn't duplicate the code that set up the array, have a separate method that figures out what set of data you want and call it from both places.
    – Psycho
    Nov 16, 2013 at 22:00
  • ty, I will be doing that, as well as a few other changes, to the code. Just doing as little as possible for the moment to fix the bug.
    – Chuck
    Nov 16, 2013 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.