didSelectRowAtIndexPath中数组的问题。越界 [英] issue with array in didSelectRowAtIndexPath. Out of bounds

查看:201
本文介绍了didSelectRowAtIndexPath中数组的问题。越界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图推向最终视图,在该视图中将显示有关项目的全部信息。

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

我之前检查过的内容:

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

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

该问题源自
itemOverViewObj = [itemsOverViewArr objectAtIndex:[indexPath row]];

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

错误: *由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[NSMutableArray objectAtIndex:]:索引1超出范围[0 .. 0]'

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

getItemsOverview DBAccess类中的方法

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

谢谢!

推荐答案

异常消息说明了一切:您正在尝试访问 itemsOverViewArr 中的第二项(索引1),但是该数组仅包含一项(索引0)。

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).

为什么这样,只有这样才能告诉您,因为您没有向我们显示相关代码。但是肯定看起来像 [access getItemsOverView:itemObj.itemID] 返回一个包含1个元素的数组。

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.

此外,您的代码中至少有一次内存泄漏。 NSMutableArray * itemsOverViewArr = [[NSMutableArray alloc] initWithCapacity:1]; 创建的数组在行 itemsOverViewArr = [access getItemsOverView: itemObj.itemID]; ,因此无法释放。您稍后发布 itemsOverViewArr 的事实可能也是内存管理错误(在这种情况下为过度发布),因为 getItemsOverView:应该返回一个自动释放的对象。 (顺便说一句,您不应将这样的方法命名为 get ... ,因为此命名约定在Cocoa中暗示该方法通过指向该方法参数之一的指针返回其结果。)

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.)

这篇关于didSelectRowAtIndexPath中数组的问题。越界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆