问题与didSelectRowAtIndexPath中的数组。 越界

我正在尝试推出最后的视图,其中显示有关项目的全部信息。

- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Item *itemObj; //object that holds data for cell for current view ItemShow *itemOverViewObj; //data object for the next view if(atableView==self.tableView) { //ordinary tabeView itemObj=[self.itemsArray objectAtIndex:[indexPath row]]; } else { //filtered with search tableView itemObj=[self.filteredItems objectAtIndex:[indexPath row]]; } //The array which will store the data for overview NSMutableArray *itemsOverViewArr=[[NSMutableArray alloc]initWithCapacity:1]; DBAccess *access=[[DBAccess alloc]init]; //method for fetching data from db and inserting into array itemsOverViewArr=[access getItemsOverView:itemObj.itemID]; [access closeDataBase]; [access release]; //here is the evil. itemOverViewObj=[itemsOverViewArr objectAtIndex:[indexPath row]]; [itemsOverViewArr release]; ItemOverView *iov=[[ItemOverView alloc]initWithNibName:@"ItemOverView" bundle:nil]; iov.title=self.nominal; [iov setItemShow:itemOverViewObj]; [self.navigationController pushViewController:iov animated:YES]; [iov release]; } 

我之前检查过的东西:

在getItemsOverView方法中select适用于100%。 itemObj.itemID获取正确的int。

发生在itemOverViewObj = [itemsOverViewArr objectAtIndex:[indexPath row]]的问题;

错误: *终止应用程序由于未捕获的exception'NSRangeException',原因:'* – [NSMutableArray objectAtIndex:]:索引1超越界限[ 0..0 ]'

DBAccess类中的getItemsOverview方法

 -(NSMutableArray*)getItemsOverView:(int)itemID { NSMutableArray *itemsArray=[[[NSMutableArray alloc]init]autorelease]; const char *sqlItems=sqlite3_mprintf("SELECT itm.itemID,itm.itemYear,itm.rarity,dateComment,itm.mintage,dc.dateCode as dateCode,iaval.availability as avalibility,iaval.quality as quality,itm.Mintmark,itm.specialRemark\ from items itm\ join itemAvailability iaval on iaval.itemID=itm.itemID\ join dateCultures dc ON dc.dateCultureID=itm.dateCulture\ WHERE itm.itemID=%i",itemID); sqlite3_stmt *statement; int sqlResult = sqlite3_prepare_v2(database, sqlItems, -1, &statement, NULL); if ( sqlResult== SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { ItemShow *itemShow=[[ItemShow alloc]init]; itemShow.itemID=sqlite3_column_int(statement, 0); char *itemYear=(char *)sqlite3_column_text(statement, 1); char *mintMark=(char *)sqlite3_column_text(statement, 2); char *masterMark=(char *)sqlite3_column_text(statement, 3); itemShow.rarity=sqlite3_column_int(statement, 4); itemShow.availability=sqlite3_column_int(statement, 5); itemShow.quality=sqlite3_column_int(statement, 6); char *mintage=(char *)sqlite3_column_text(statement, 7); itemShow.itemYear=(itemYear)?[NSString stringWithUTF8String:itemYear]:@""; itemShow.mintMark=(mintMark)?[NSString stringWithUTF8String:mintMark]:@""; itemShow.masterMark=(masterMark)?[NSString stringWithUTF8String:masterMark]:@""; itemShow.mintage=(mintage)?[NSString stringWithUTF8String:mintage]:@"Unknown"; [itemsArray addObject:itemShow]; [itemShow release]; } sqlite3_finalize(statement); } else { [self dbConnectionError]; } return itemsArray; } 

先谢谢你

exception消息说明了这一切:您正在尝试访问itemsOverViewArr的第二个项目(索引1),但此数组仅包含一个项目(索引0)。

既然你没有向我们展示相关的代码,为什么只能这样呢? 但是它肯定看起来像[access getItemsOverView:itemObj.itemID]返回一个包含1个元素的数组。

此外,您的代码中至less有一个内存泄漏。 由NSMutableArray *itemsOverViewArr=[[NSMutableArray alloc]initWithCapacity:1];创build的数组NSMutableArray *itemsOverViewArr=[[NSMutableArray alloc]initWithCapacity:1]; itemsOverViewArr=[access getItemsOverView:itemObj.itemID];之后不再可到达itemsOverViewArr=[access getItemsOverView:itemObj.itemID]; 因此不能被释放。 事实上,你以后发布itemsOverViewArr可能也是一个内存pipe理错误(在这种情况下itemsOverViewArr释放),因为getItemsOverView:应该返回一个自动释放对象。 (顺便说一下,你不应该命名这样的方法get...因为这个命名约定隐含在Cocoa中,该方法通过指向该方法参数的指针返回其结果。)

什么:

 itemsOverViewArr=[access getItemsOverView:itemObj.itemID]; 

做?

如果它返回一个数组,像这样声明你的NSMutableArray:

 NSMutableArray * itemsOverViewArr = [[NSMutableArray alloc] initWithArray: [access getItemsOverView:itemObj.itemID]]; 

然后还要确保在引用它时确保该可变数组中至less有一个项目。

换一种说法:

 if(itemsOverViewArr && ([itemsOverViewArr count] > 1)) { itemOverViewObj=[itemsOverViewArr objectAtIndex:[indexPath row]]; ... ... 

看起来像你使用委托多个tableView

 if(atableView==self.tableView) 

确保你在这里访问正确的tableview:

 itemOverViewObj=[itemsOverViewArr objectAtIndex:[indexPath row]]; 

或者你会得到错误的索引到数组,索引0可能只有一个项目,这将使索引1超出界限。