UITableView中的多个部分导致NSRangeException?

时间:2012-01-24 18:37:21

标签: objective-c cocoa-touch ios5

我正在制作一个导航类别树的简单测试应用程序。每当我尝试将表视图分成多个部分时,我得到的SIGABRT没有有用的调用堆栈,而且:

  

由于未捕获的异常'NSRangeException'而终止应用程序,原因:'*** - [__ NSArrayI objectAtIndex:]:索引1超出边界[0 .. 0]'

我到处都有断点。当numberOfSectionsInTableView返回1时,一切正常,但当它返回2时,它就会到达 - 没有其他断点被击中。我在objectAtIndex的每个电话都设置了一个断点,我不知道问题出在哪里。

这是我的实现代码:

//
//  MasterViewController.m
//  Vingo
//
//  Created by ryan on 12-01-23.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MasterViewController.h"
#import "LinkViewController.h"

@implementation MasterViewController

@synthesize detailViewController = _detailViewController;

- (void)awakeFromNib
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    }
    [super awakeFromNib];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    }

    self.navigationController.navigationBar.tintColor = [UIColor orangeColor];

    categories = [NSArray arrayWithObjects:[[VingoCategory alloc] initWithId:0 andName:@"A category"], nil];
    links = [NSArray arrayWithObjects:[[VingoLink alloc] initWithId:0 andName:@"A link"], nil];
    //links = [NSArray array]; // works
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

#pragma mark - Table data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [categories count] && section == 0 ?
            [categories count]
        :   [links count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if([categories count]) {
        if([links count]) {
            return 2;
        }

        return 1;
    }

    if([links count]) {
        return 1;
    }

    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [categories count] && section == 0 ?
            @"Categories"
        :   @"Links";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    // Try to get a reusable cell:
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    if([categories count] && indexPath.section == 0) {
        NSLog(@"Looking for item %d in categories", indexPath.row);
        VingoCategory *category = [categories objectAtIndex:indexPath.row];
        cell.textLabel.text = category.name;
        cell.textLabel.tag = category.id;
    } else {
        NSLog(@"Looking for item %d in links", indexPath.row);
        VingoLink *link = [links objectAtIndex:indexPath.row];
        cell.textLabel.text = link.name;
        cell.textLabel.tag = link.id;
    }

    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if([categories count] && indexPath.section == 0) {

    } else {
        LinkViewController *linkController = [[LinkViewController alloc] init];
        VingoLink *link = [links objectAtIndex:indexPath.row];
        linkController.navigationItem.title = link.name;
        [self.navigationController pushViewController:linkController animated:YES];
    }

    return indexPath;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

@end

有人能找到问题吗?我希望这是显而易见的,我缺少了,而不是一个严重的问题。

我对两个相同的NSArray的初始化位于viewDidLoad

1 个答案:

答案 0 :(得分:0)

我最好的猜测是你没有添加新的部分和行?

请参阅UITableView的{​​{1}}和insertSections:

当然,你可能在其他地方这样做,我没有意识到......