-3

次のエラーが発生します。

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
*** First throw call stack:
(0x372df88f 0x357e5259 0x372df789 0x372df7ab 0x372379c3 0x2a815d 0x2a6dad 0x339ec0a3 0x339eb181 0x339ea90b 0x3398f0df 0x3723e1fb 0x34216aa5 0x342166bd 0x3421a843 0x3421a57f 0x342124b9 0x372b3b1b 0x372b1d57 0x372b20b1 0x372354a5 0x3723536d 0x32142439 0x339b9e7d 0x3bf7 0x3ba8)
terminate called throwing an exceptionkill

これが私が持っているコードです:

#pragma mark -
#pragma mark Table view data source



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView.tag == kTableViewCatalog){
        NSLog(@"CATALOG IMAGES IS %d",[self.catalogImages_ count] );
        return [self.catalogImages_ count];
    } else if (tableView.tag == kTableViewFriends){
         NSLog(@"USER IMAGES IS %d",[self.userProfileImages_ count] );
        return [self.userProfileImages_ count];
    } else if (tableView.tag == kTableViewNews) {
         NSLog(@"STORIES IMAGES IS %d",[self.stories_ count] );
        return [self.stories_ count];
    }

    NSLog(@"OH NOO");
    return 0;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];

    NSString *CellIdentifier = @"NewsItemCellIdentifier";
    NewsItemCell *cell = (NewsItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NewsItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        //cell.newsRackController = self.newsRackController;
    }

    if (tableView.tag == kTableViewNews){
         NewsStory *story = [self.stories_ objectAtIndex:row];
        [cell setNewsStory:story];
        return cell;
    } else if (tableView.tag == kTableViewCatalog || tableView.tag == kTableViewFriends){
        [cell setNewsStory:nil];
        NewsStory *story = [NewsStory new];
        if (tableView.tag == kTableViewCatalog){
             NSLog(@"SETTING CELL FOR CATALOG");
            [cell setImageWithURL:[self.catalogImages_ objectAtIndex:indexPath.row]];
        } else if (tableView.tag == kTableViewFriends){
            NSLog(@"SETTING CELL FOR FRIENDS");
            [cell setImageWithURL:[self.userProfileImages_ objectAtIndex:indexPath.row]];
        }

        [story release];
        return cell;
    } 

    return nil;
}

#pragma mark -
#pragma mark Table view delegate

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    self.currentIndexPath = indexPath;
//    [self tappedAtNewsItem:[self.stories_ objectAtIndex:[indexPath row]]];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath row] < [self.stories_ count]) {
        return pageHeight;
    }
    return 0;
}

したがって、UITableViewCellサブクラス内にtableViewがあり、何らかの理由でスクロールしているときに上記のエラーが発生します。どうすればこれを解決できますか?

4

1 に答える 1

0
#pragma mark -
#pragma mark Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView.tag == kTableViewCatalog){
        NSLog(@"CATALOG IMAGES IS %d",[self.catalogImages_ count] ); 
//Check here how many elements in this array [self.catalogImages_] ?

        return [self.catalogImages_ count];
    } else if (tableView.tag == kTableViewFriends){
         NSLog(@"USER IMAGES IS %d",[self.userProfileImages_ count] );
//Check here how many elements in this array [self.userProfileImages_] ?

        return [self.userProfileImages_ count];
    } else if (tableView.tag == kTableViewNews) {
         NSLog(@"STORIES IMAGES IS %d",[self.stories_ count] );
//Check here how many elements in this array [self.stories_] ?

        return [self.stories_ count];
    }

else{
    NSLog(@"OH NOO");
    return 0;
}

}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];

    NSString *CellIdentifier = @"NewsItemCellIdentifier";
    NewsItemCell *cell = (NewsItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NewsItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        //cell.newsRackController = self.newsRackController;
    }

    if (tableView.tag == kTableViewNews){
         NewsStory *story = [self.stories_ objectAtIndex:row];
//Check here how many elements in this array [self.stories_] ?

        [cell setNewsStory:story];
        return cell;
    } else if (tableView.tag == kTableViewCatalog || tableView.tag == kTableViewFriends){
        [cell setNewsStory:nil];
        NewsStory *story = [NewsStory new];
        if (tableView.tag == kTableViewCatalog){
             NSLog(@"SETTING CELL FOR CATALOG");
            [cell setImageWithURL:[self.catalogImages_ objectAtIndex:indexPath.row]];
//Check here how many elements in this array [self.catalogImages_] ?

        } else if (tableView.tag == kTableViewFriends){
            NSLog(@"SETTING CELL FOR FRIENDS");
            [cell setImageWithURL:[self.userProfileImages_ objectAtIndex:indexPath.row]];
//Check here how many elements in this array [self.userProfileImages_] ?

        }

        [story release];
        return cell;
    } 

    return nil;
}

#pragma mark -
#pragma mark Table view delegate

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    self.currentIndexPath = indexPath;
//    [self tappedAtNewsItem:[self.stories_ objectAtIndex:[indexPath row]]];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath row] < [self.stories_ count]) {
        return pageHeight;
    }
    return 0;
}

// Check every where the size of each array. Actually the problem with you is this, the size is low of given array and you are trying to access beyond size. 

// numberOfRowsInSection ---> fixed with hard coded -- return 5; in each case.
 and fill your all arrays at least with 6 or 7 or more elements and then try .
I think you will get the main point.
于 2012-05-01T04:51:22.890 に答える