Search code examples
objective-ciosnsrangeexception

NSRangeException from UITableViewController


I can't solve the mystery of my NSRangeException.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_countyList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Inside masterview cellforrowatindexpath.\n");

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [_countyList objectAtIndex:indexPath.row];

    return cell;
}

The exception is thrown when I have numberOfSectionsInTableView return 1 (there should be 1 section), but when I return 0 sections it does not throw an exception (it also does not display my data). When the code runs, it never calls cellForRowAtIndexPath. The debugger takes me to the main function and I cannot see where I am accessing a NSArray for the life of me.
Any help or debugging tips would be greatly appreciated...


Solution

  • If you are using storyboard check this post.

    iOS Xcode 4.2 Master-Detail Application Template Throwing NSRangeException

    The problem you are describing look similar to that one.