iOS NSRangeException,Index Beyond Bounds,NSMutable Array

时间:2011-07-07 19:40:03

标签: iphone objective-c nsmutablearray terminate

在改编iPhone应用程序项目之后,我负责在更新后对其进行调试。不是一个客观的程序员,我不知道从哪里开始。任何帮助将不胜感激。

这是调试输出:

2011-07-07 15:27:44.333 App[5836:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013d0be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x015255c2 objc_exception_throw + 47
    2   CoreFoundation                      0x013c66e5 -[__NSArrayM objectAtIndex:] + 261
    3   CollegeFootballAlerts               0x00034892 -[SMStandings tableView:cellForRowAtIndexPath:] + 397
    ...
)
terminate called after throwing an instance of 'NSException'

断点位于teamsInfo =:

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

    SMStandingsTableViewCell * cell = (SMStandingsTableViewCell *)[tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];
    if( cell == nil ){
        [[NSBundle mainBundle] loadNibNamed:@"SMStandingsTableViewCell" owner:self options:nil];
        cell = standingsTableCell;
        standingsTableCell = nil;
    }
    SMTeamsInfo * teamInfo;
        NSMutableArray * teamsInGroup = [allGroups objectForKey:[allKeys objectAtIndex:indexPath.section]];
        NSLog(@"TEAMS IN GROUP %@ :: %d", teamsInGroup, indexPath.row);
        teamInfo = [teamsInGroup objectAtIndex:indexPath.row];

    [[cell teamName] setText:[teamInfo nameEN]];
    [[cell teamFlag] setImage:[teamInfo teamFlag]];
    NSString * str;
    if([segmentedControl selectedSegmentIndex] == 0) {               // for conference tab wonconf - lostconf  combination is used 
        str= [NSString stringWithFormat:@"%@",[teamInfo wonconf]];
        str = [str stringByAppendingString:@"-"];
        str = [str stringByAppendingFormat:@"%@",[teamInfo lostconf]];
    }else {                                                          // for overall tab wonconf - lostconf  combination is used 
        str= [NSString stringWithFormat:@"%@",[teamInfo wonall]];
        str = [str stringByAppendingString:@"-"];
        str = [str stringByAppendingFormat:@"%@",[teamInfo lostall]];
    }

    [[cell currentStanding ]setText:str];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;    
}

和numberOfRowsInSection方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray * array = [allGroups allKeys];
    NSLog(@"NO OF ROWS IN SECTION #%d : %d", section, [[allGroups objectForKey:[array objectAtIndex:section]] count]);  
    return [[allGroups objectForKey:[array objectAtIndex:section]] count];
}

和该方法的输出:

2011-07-08 17:46:13.837 App[7513:207] NO OF ROWS IN SECTION #1 : 7
2011-07-08 17:46:13.837 App[7513:207] NO OF ROWS IN SECTION #0 : 6

不确定为什么第一节第一个进入......行数是相反的。第0部分应为7,第1部分应为6,这似乎是问题的根源。

2 个答案:

答案 0 :(得分:3)

你在调试时是否在xcode中得到了这个?如果是这样,它应该自动将你带到它发生的那条线上。

如果没有,它会发生在SMStanding的{​​{1}}方法中某些地方。并且看起来像一个通用数组超出范围的问题。如果您需要帮助,请使用该方法中的代码编辑您的问题。

编辑:一般来说,对于这种堆栈跟踪,我尝试找到一个类名称/方法,它是我做的并从那里开始。

答案 1 :(得分:1)

这意味着您有一个包含6个项目的NSArray,并且您要求索引6不会退出,只有索引0..5包含在内。

这种情况发生在您的表格视图委托方法[SMStandings tableView:cellForRowAtIndexPath:]中,您可能会返回一个数字太高的数字- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

相关问题