issue with array in didSelectRowAtIndexPath. Out of bounds

社会主义新天地 提交于 2019-12-25 18:37:04

问题


I am trying to push to final view where the whole information about item will be shown.

- (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];
}

What I've checked before:

Select in the method getItemsOverView works for 100%. itemObj.itemID fetches the right int.

The problem originating in itemOverViewObj=[itemsOverViewArr objectAtIndex:[indexPath row]];

The error: * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

getItemsOverview meth in DBAccess Class

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

Thank you in advance


回答1:


The exception message says it all: You are trying to access the second item (index 1) in itemsOverViewArr but this array only contains one item (index 0).

Why that is so only you can tell since you don't show us the relevant code. But it sure looks like [access getItemsOverView:itemObj.itemID] returns an array containing 1 element.

Also, you have at least one memory leak in your code. The array created by NSMutableArray *itemsOverViewArr=[[NSMutableArray alloc]initWithCapacity:1]; is no longer reachable after the line itemsOverViewArr=[access getItemsOverView:itemObj.itemID]; and thus cannot be released. The fact that you are releasing itemsOverViewArr later is probably also a memory management error (in this case an overrelease) because getItemsOverView: should return an autoreleased object. (Btw, you should not name such methods get... as this naming convention implies in Cocoa that the method returns its results via a pointer to one of the method's arguments.)




回答2:


What does:

itemsOverViewArr=[access getItemsOverView:itemObj.itemID];

do?

If it returns an array, declare your NSMutableArray like this:

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

And then also make sure, when referencing it, to make certain that there's at least one item in that mutable array.

In other words:

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



回答3:


Looks like your using the delegate for more than one tableView

if(atableView==self.tableView)

Make sure you accessing the right tableview here:

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

or you will get the wrong index into the array, which may only have one item at index 0, which would make index 1 beyond bounds.



来源:https://stackoverflow.com/questions/8778178/issue-with-array-in-didselectrowatindexpath-out-of-bounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!