2012-05-01 29 views
-3

私は、次のエラーを取得しています:問題:

#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を持っている、といくつかの理由でI:

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

ここで私が持っているコードです私は上記のエラーをスクロールしています。これをどうすれば解決できますか?

+5

本当にあなた自身で解決しようとしましたか?あまりにも難しくはありません、あなたのテーブルビューの1つは、あなたが持っているより多くのオブジェクトを配列内に期待し、空の配列の最初のオブジェクトにアクセスしようとしています。あなたの配列には何がありますか?あなたはそれよりも簡単です。 – 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.