4

This is my code (from a Core Data tutorial):

[eventsArray insertObject:event atIndex:0];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

The third line throws an exception:

2011-05-12 13:13:33.740 Locations[8332:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x010145a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01168313 objc_exception_throw + 44
    2   CoreFoundation                      0x0100a0a5 -[__NSArrayM objectAtIndex:] + 261
    3   UIKit                               0x0010d5b3 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 6156
    4   UIKit                               0x000fcd36 -[UITableView insertRowsAtIndexPaths:withRowAnimation:] + 56
    5   Locations                           0x00003462 -[RootViewController addEvent] + 690

I'm new to iPhone development, and I can't understand that does it mean. I'm inserting at the zero index of a tableView, so I have no idea why does not it work with an empty array. Please clarify this to me


stacktrace

eventsArray is an array I populate table view from (probably. at least it is used in cellForRowAtIndexPath)


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // A date formatter for the time stamp.
    static NSDateFormatter *dateFormatter = nil;
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    }

    // A number formatter for the latitude and longitude.
    static NSNumberFormatter *numberFormatter = nil;
    if (numberFormatter == nil) {
        numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
        [numberFormatter setMaximumFractionDigits:3];
    }

    static NSString *CellIdentifier = @"Cell";

    // Dequeue or create a new cell.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    Event *event = (Event *)[eventsArray objectAtIndex:indexPath.row];

    cell.textLabel.text = [dateFormatter stringFromDate:[event creationDate]];

    NSString *string = [NSString stringWithFormat:@"%@, %@",
                        [numberFormatter stringFromNumber:[event latitude]],
                        [numberFormatter stringFromNumber:[event longitude]]];
    cell.detailTextLabel.text = string;

    return cell;
}
Valentin Golev
  • 9,965
  • 10
  • 60
  • 84

4 Answers4

3

Had the same problem. Just need to tell the tableview that it has 1 section to add too. It defaults to 0 if you don't update it.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
binarycube
  • 121
  • 4
  • Great, I think the iOS CoreData tutorial from Apple was missing this and it was starting to get frustrating. – cfeduke Jun 27 '11 at 19:13
0

Can you please check the data source used to load the table view? It appears to an empty array.

PgmFreek
  • 6,374
  • 3
  • 36
  • 47
  • well, it is supposed to be empty right now because it's the first time the application starts – Valentin Golev May 12 '11 at 09:42
  • Then you have to load the data source first then load the table view. – PgmFreek May 12 '11 at 09:44
  • I'm not sure what you mean. I've added a screenshot to the post – Valentin Golev May 12 '11 at 09:57
  • according to screenshot, it exists and has items. I'm really new to Objective C and I'm not really sure I understand what "allocated" means – Valentin Golev May 12 '11 at 10:02
  • Can you add eventsArray = [[NSMutableArray alloc] init]; before [eventsArray insertObject:event atIndex:0]; – PgmFreek May 12 '11 at 10:06
  • same: http://cl.ly/1v2V3T0q2O3Y111h1j3f I'd like to make a note that the error is not about inserting into eventsArray, and eventsArray is fine - take a look at variables on the first screenshot – Valentin Golev May 12 '11 at 10:09
  • done, added to the post.http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008305-CH1-SW1 I copypasted everything from there – Valentin Golev May 12 '11 at 10:13
  • Did you run the same code ? I have no issue here. I try to run the code it worked for me. – PgmFreek May 12 '11 at 10:28
  • I'm running XCode 4, maybe that can be issue? If it's not, I'd try posting it somewhere – Valentin Golev May 12 '11 at 12:27
  • Did you turn on the GPS of the device before running the app? – PgmFreek May 12 '11 at 12:28
  • sorry, I found my mistake. it looks like `numberOfSectionsInTableView` was returning 0 for some reason. I don't know why there was `return 0;`, maybe I did it accidentally – Valentin Golev May 12 '11 at 12:44
0

It appears to me that your eventsArray is filled, and working, but the update on the tableview datasource doesn't work properly. This could mean that the datasource is not properly set, or perhaps your tableview datasource methods return wrong values?

What is the code behind the following methods in your subclass? (assuming you correctly conform to the UITableViewDataSource protocol).

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
Jake
  • 3,973
  • 24
  • 36
  • 1
    I found my mistake. it looks like numberOfSectionsInTableView was returning 0 for some reason. I don't know why there was return 0;, maybe I did it accidentally – Valentin Golev May 12 '11 at 12:44
0

Try adding this to the viewdidLoad in your controller:

eventsArray = [[NSMutableArray alloc] init];
Ayrad
  • 3,996
  • 8
  • 45
  • 86