2010-11-30 66 views
0

我有一个“终止应用程序由于未捕获的异常'NSRangeException',原因:'*** - [NSArray objectAtIndex:]:索引24越界[0 .. 23]'” 我我不知道如何把我的.m放在这里,而不是很长。请让我知道我可以如何协助解决这个问题。 在此先感谢!iphone sdk超越界限NSException

代码:

#import "RootViewController.h" 
#import "FSDAppDelegate.h" 
#import "DetailViewController.h" 
@implementation RootViewController 

#pragma mark - 
#pragma mark Synthesizers 

@synthesize mainTableView; 
@synthesize contentsList; 
@synthesize descArray; 
@synthesize bannerImages; 
@synthesize childController; 
@synthesize searchResults; 
@synthesize savedSearchTerm; 


#pragma mark - 
#pragma mark View methods 

- (void)viewDidLoad 
{ 
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 


    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Descriptions" ofType:@"plist"]; 

    NSArray *desc = [[NSArray alloc] initWithContentsOfFile:filePath]; 
    self.descArray = desc; 
    [desc release]; 

    UIImage *texas = [UIImage imageNamed:@"1andtexas.jpg"]; 
    UIImage *cali = [UIImage imageNamed:@"2andcalifornia.jpg"]; 
    UIImage *ny = [UIImage imageNamed:@"3andnewyork.jpg"]; 
    UIImage *tmark = [UIImage imageNamed:@"1Tmark.jpg"]; 
    UIImage *walkie = [UIImage imageNamed:@"Walkietalkie.jpg"]; 
    UIImage *onekh = [UIImage imageNamed:@"Tracingpaper.jpg"]; 
    UIImage *onetwonine = [UIImage imageNamed:@"diffusion.jpg"]; 
    UIImage *greycard = [UIImage imageNamed:@"Greycard.jpg"]; 
    UIImage *oneeighty = [UIImage imageNamed:@"Oneeighty.jpg"]; 
    UIImage *two = [UIImage imageNamed:@"Two.jpg"]; 
    UIImage *twofourteen = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofifteen = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twosixteen = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twotwenty = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twotwentynine = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofifty = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofiftyone = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofiftytwo = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofiftythree = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofiftyfour = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofiftyfive = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofiftysix = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofiftyseven = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *twofiftyeight = [UIImage imageNamed:@"Diffusion.jpg"]; 
    UIImage *thirtytwohk = [UIImage imageNamed:@"3200k.jpg"]; 



    NSArray *banners = [[NSArray alloc] initWithObjects: 
         texas, 
         cali, 
         ny, 
         tmark, 
         walkie, 
         onekh, 
         onetwonine, 
         greycard, 
         oneeighty, 
         two, 
         twofourteen, 
         twofifteen, 
         twosixteen, 
         twotwenty, 
         twotwentynine, 
         twofifty, 
         twofiftyone, 
         twofiftytwo, 
         twofiftythree, 
         twofiftyfour, 
         twofiftyfive, 
         twofiftysix, 
         twofiftyseven, 
         twofiftyeight, 
         thirtytwohk, 
         nil]; 

    self.bannerImages = banners; 

    [banners release]; 


    [super viewDidLoad]; 



    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: 
          @"#1 or NUMBER ONE", 
          @"#2 or NUMBER TWO", 
          @"#3 or NUMBER THREE", 
          @"1 or ONE", 
          @"10-100,10-1,10-200,10-2,10-4 and 20", 
          @"1000H PAPER", 
          @"129 or HEAVY FROST", 
          @"18% GRAY or GREY CARD", 
          @"180 DEGREE RULE", 
          @"2 or TWO", 
          @"214 or FULL TOUGH SPUN", 
          @"215 or HALF TOUGH SPUN", 
          @"216 or FULL WHITE DIFFUSION", 
          @"220 or WHITE FROST", 
          @"229 or QUARTER TOUGH SPUN", 
          @"250 or HALF WHITE DIFFUSION", 
          @"251 or QUARTER WHITE DIFFUSION", 
          @"252 or EIGHTH WHITE DIFFUSION", 
          @"253 or FULL HAMPSHIRE", 
          @"254 or NEW HAMPSHIRE", 
          @"255 or HOLLYWOOD FROST", 
          @"256 or HALF HAMPSHIRE FROST", 
          @"257 or QUARTER HAMPSHIRE FROST", 
          @"258 or EIGHTH HAMPSHIRE FROST", 
          @"3200K or TUNGSTEN", 
          nil]; 
    [self setContentsList:array]; 
    [array release], array = nil; 

    if ([self savedSearchTerm]) 
    { 
     [[[self searchDisplayController] searchBar] setText:[self savedSearchTerm]]; 
    } 

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    [super viewWillAppear:animated]; 

    [[self mainTableView] reloadData]; 

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 
} 

#pragma mark - 
#pragma mark Table view datasource methods 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 

    NSMutableArray *toBeReturned = [[NSMutableArray alloc]init]; 
    for(char c = 'A'; c <= 'Z'; c++) [toBeReturned addObject:[NSString stringWithFormat:@"%c",c]]; 
    return toBeReturned; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    NSInteger rows; 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) 
     rows = [[self searchResults] count]; 
    else 
     rows = [[self contentsList] count]; 

    return [contentsList count]; 
    return rows; 

    NSLog(@"rows is: %d", rows); 
    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 
} 

-(UITableViewCell *) tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    NSInteger row = [indexPath row]; 
    NSString *contentForThisRow = nil; 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) 
     contentForThisRow = [[self searchResults] objectAtIndex:row]; 
    else 
     contentForThisRow = [[self contentsList] objectAtIndex:row]; 

    static NSString *CellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    [[cell textLabel] setText:contentForThisRow]; 

    cell.textLabel.text = [contentsList objectAtIndex:indexPath.row]; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 

    return cell; 
} 

#pragma mark - 
#pragma mark Table view delegate methods 

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 50; 
} 

-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 
{ 
    return @"Film Set Decipher"; 
} 

-(void) tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    FSDAppDelegate *delegate = (FSDAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 
    detailVC.banner = [bannerImages objectAtIndex:indexPath.row]; 
    detailVC.description = [descArray objectAtIndex:indexPath.row]; 
    detailVC.title = [contentsList objectAtIndex:indexPath.row]; 
    self.childController = detailVC; 
    [delegate.navigationController pushViewController:childController animated:YES]; 

    [detailVC release]; 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 
} 

#pragma mark - 
#pragma mark UISearchDisplayController Delegate Methods 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    [self handleSearchForTerm:searchString]; 

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 
    return YES; 
} 

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller 
{ 
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    [self setSavedSearchTerm:nil]; 

    [[self mainTableView] reloadData]; 

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 
} 
#pragma mark - 
#pragma mark Memory Management 

- (void)didReceiveMemoryWarning { 
    NSLog(@"Memory warning!"); 
    [super didReceiveMemoryWarning]; 
} 
- (void)dealloc 
{ 
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    [mainTableView release]; mainTableView = nil; 
    [contentsList release]; contentsList = nil; 
    [descArray release]; descArray = nil; 
    [bannerImages release]; bannerImages = nil; 
    [childController release]; childController = nil; 
    [searchResults release], searchResults = nil; 
    [savedSearchTerm release], savedSearchTerm = nil; 

    [super dealloc]; 

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 
} 

- (void)viewDidUnload { 
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    self.mainTableView = nil; 
    self.contentsList = nil; 
    self.descArray = nil; 
    self.bannerImages = nil; 
    self.childController = nil; 

    [super viewDidUnload]; 

    [self setSavedSearchTerm:[[[self searchDisplayController] searchBar] text]]; 

    [self setSearchResults:nil]; 

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 

} 

- (void)handleSearchForTerm:(NSString *)searchTerm 
{ 
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    [self setSavedSearchTerm:searchTerm]; 

    if ([self searchResults] == nil) 
    { 
     NSMutableArray *array = [[NSMutableArray alloc] init]; 
     [self setSearchResults:array]; 
     [array release], array = nil; 
    } 

    [[self searchResults] removeAllObjects]; 

    if ([[self savedSearchTerm] length] != 0) 
    { 
     for (NSString *currentString in [self contentsList]) 
     { 
      if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) 
      { 
       [[self searchResults] addObject:currentString]; 
      } 
     } 
    } 

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__); 
} 

@end 

原来,问题是,图像是不是所有复制到目标>复制包资源文件夹,那是什么使它崩溃!?!?太感谢了!

+0

你能正确格式化代码吗?这将更容易发现问题。 – 2010-11-30 05:09:58

回答

0

错误提示该数组有24个元素,但您正尝试访问第25个元素。所以你需要发布试图访问第25个元素的代码。或者找出为什么它试图找到24元素数组中的第25个元素。

编辑:

你应该在第一线左右的tableView设置断点:didSelectRowAtIndexPath方法:

然后在GDB>提示符下键入:

po descArray 

或者只是:

p(int) [descArray count] 

或者在变量窗格中查看有多少项目实际在descArray在那一点。如果需要重复其他两个阵列。

再次编辑:

您有其他问题(可能无关的)在我们的tableView:numberOfRowsInSection:

您返回contentList算上所有的时间。包括NSLogs在内的线路将永远不会到达。只要您在C中第一次返回,您将立即从该方法返回。我不认为这是你的直接问题,但是当你开始搜索工作时会出现另一个问题。物体在这些 bannerImages descArray contentsList

这些阵列不必对象等于表中的视图中的行数的数

+0

我跑调试器本身是什么东西,这就是弹出: – Draken182 2010-11-30 03:42:40

0

检查数。 (你必须检查你是否准备好你的数组,然后正确地使用tableview数据源和委托方法)。或者如果你没有得到解决方案,请把所有的代码放在这里。