cellForRowAtIndexPath 的问题:

Posted

技术标签:

【中文标题】cellForRowAtIndexPath 的问题:【英文标题】:Issue with cellForRowAtIndexPath: 【发布时间】:2012-05-01 03:09:51 【问题描述】:

我收到以下错误:

*** 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,由于某种原因,当我滚动时,我得到了上面的错误。我该如何解决?

【问题讨论】:

您真的尝试过自己修复它吗?似乎不太难,您的一个表视图期望数组中的对象比您拥有的对象多,并且您正在尝试访问空数组的第一个对象,您只需要找到不一致之处,我们不知道你的阵列中有什么,你做,所以你比我们更容易。 能否包含显示每个数组大小的日志?例如“目录图像为 n”等 好吧,我删除了错误的答案,这很丢脸......但我仍然想知道你为什么使用self.catalogImages_?有点奇怪。我认为应该是catalogImages_self.catalogImages 【参考方案1】:
#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.

【讨论】:

以上是关于cellForRowAtIndexPath 的问题:的主要内容,如果未能解决你的问题,请参考以下文章