2012-01-08 9 views
-1

私はアイテムの全情報が表示される最終ビューにプッシュしようとしています。 100%のために働くgetItemsOverView方法で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]; 
} 

。 itemObj.itemIDは正しいintを取得します。

で問題が発生しました。itemOverViewObj = [itemsOverViewArr objectAtIndex:[indexPath row]];

エラー:*によりキャッチされない例外 'NSRangeException'、理由にアプリを終了: '* - [NSMutableArrayのobjectAtIndex:]:境界を超えてインデックス1 [0 .. 0]' DBACCESSで

getItemsOverviewのメタあなたはitemsOverViewArrで2番目の項目(インデックス1)にアクセスしようとしているが、この配列は唯一の含まれています。クラス

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

は、例外メッセージはそれをすべて言う事前に

答えて

2

をありがとうe項目(インデックス0)。

それはそうですなぜあなたは私たちに関連するコードを示していないだけなので、あなたが言うことができます。しかし、それは確かに[access getItemsOverView:itemObj.itemID]のように見える1要素を含む配列を返します。

また、あなたはあなたのコード内の少なくとも1つのメモリリークを持っています。 NSMutableArray *itemsOverViewArr=[[NSMutableArray alloc]initWithCapacity:1];によって作成された配列は、itemsOverViewArr=[access getItemsOverView:itemObj.itemID];行以降はアクセスできなくなり、解放することはできません。 getItemsOverView:は、自動解放オブジェクトを返す必要がありますので、あなたが後でitemsOverViewArrをリリースしているという事実も、おそらくメモリ管理エラー(この場合はoverrelease)です。 (ところで、あなたはこの命名規則などの方法get...に名前を付けるべきではないメソッドは、メソッドの引数の1へのポインタを経由してその結果を返すことをココアにを意味します。)

+0

itemsOverViewArr = [アクセスgetItemsOverView:itemObj.itemID];の後に空でない; NSLog(@ "%d"、[itemsOverViewArr count])を書きました。それが本当にいっぱいになっているかどうかを確認してください。私は1を得ている。だからそれはいっぱいになっている。 – NCFUSN

+0

@Nathan 1は1つの要素を意味し、objectAtIndex:0を使用して配列のインデックス付けを行うためにはゼロベースで扱う必要があります。しかし元の見積もりでは配列が空であることが示唆されています - あなたの見積りが間違っているか、最新のNSLog結果です。 – Till

+0

ああ、確かに。 – NCFUSN

0

何をん:

itemsOverViewArr=[access getItemsOverView:itemObj.itemID]; 

は何?

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

をそして、その可変配列内の少なくとも1つの項目があることを特定するために、それを参照するときにも、確認してください。

それが配列を返す場合、このようなあなたのNSMutableArrayのを宣言します。

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

NSMutableArray * itemsOverViewArr = [[NSMutableArray alloc] initWithCapacity:1];これは私のコードですでに宣言されています。 – NCFUSN

+0

true ...上記のような宣言を答えに置き換えてもいいですし、['[itemsOverViewArr setArray:[access getItemsOverView:itemObj.itemID]];'](http: /developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/setArray :)(ドキュメントはリンクされています)。 –

+0

キャッチされていない例外 'NSInvalidArgumentException'のためアプリを終了します。理由: '[アイテム数]:クラス0x15f6cに送信されたセレクタが認識されません' – NCFUSN

0

は、あなたが複数のtableView

if(atableView==self.tableView) 

のためのデリゲートを使用してのようにあなたがここに右のテーブルビューにアクセスしていることを確認してくださいルックス:

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

または言い換えれば

配列に間違ったインデックスが付いてしまいます。インデックスはインデックス0に1つしかないかもしれません。範囲を超えてインデックス1を作成します。

+0

if(atableView == self.tableView) { //普通のtabeView itemObj = [self.itemsArray objectAtIndex:[indexPath row]]; } else { //検索テーブルビューでフィルタリングされました itemObj = [self.filteredItems objectAtIndex:[indexPath row]]; } – NCFUSN

+0

あなたのコメントの後に//悪いのは... –

+0

どちらのindexPathですか?2つのテーブルビューがある場合。 –

関連する問題