2012-05-01 66 views
-3

我收到以下錯誤:問題用的cellForRowAtIndexPath:

*** 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; 
} 

所以我有一個的tableView一個UITableViewCell子類裏面,因爲某些原因,當我滾動我得到上面的錯誤。我該如何解決這個問題?

+5

你真的試圖自己修復它嗎?看起來好像不太難,你的一個表視圖期望數組中的對象比你擁有的要多,而你試圖訪問空數組的第一個對象,你只需要找到不一致性,我們不知道你的數組中有什麼,所以你比我們容易。 – EmilioPelaez

+1

您能否包含顯示每個陣列大小的日誌?例如「CATALOG IMAGES IS n」等 –

+0

好吧,我刪除了錯誤的答案,這是羞恥的...但我仍然想知道爲什麼你使用'self.catalogImages_'?有點奇怪。我認爲它應該是'catalogImages_'或'self.catalogImages'。 – Kjuly

回答

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. 
相關問題