2016-06-27 10 views
-2

iOSでこの多次元配列を反復する方法は? このデータはJSONiOSの多次元配列から反復する

(
     (
     "burger meal", 
     "getImage.ashx?id=1", 
     150, 
     300, 
     "get a free burger" 
    ), 
     (
     "chinese combo", 
     "getImage.ashx?id=2", 
     350, 
     700, 
     "get a combo free" 
    ), 
     (
     "cheeese cake", 
     "getImage.ashx?id=3", 
     350, 
     700, 
     "get a cake free with meal" 
    ) 
) 

から来て、私は、インデックス3などでオブジェクトを使用する必要が= 150,350,350(UITableView)。

私はこの

NSArray *array = jsonArray; 

for (NSArray *newArray in array) { 
    [newArray addObjectFromArray:array]; 
} 
+0

これはあなたのコードは何ですか...何を試してみましたか? –

+0

あなたは次のようにしました:array [0] [3]? –

+1

しかし、この質問に答えるには、そのデータ(JSON?)からインスタンス変数にデータを抽出する必要があることを説明する必要があるため、反復を実際に表示する前にivarを割り当てる必要があることを説明する必要があります。完全な答えは、 "私は仕事に行くことができません"とOPに答えられます。 – Droppy

答えて

1

これは単純です。 TableViewで配列を繰り返し処理します。あなたのtableViewはすでに反復処理中です。オーバーレイに追加しないでください。そうすれば処理能力を無駄にするだけです。

まず、このようなあなたのnumberOfRowsInSectionデリゲートメソッドを変更します。私はそれがだ、ここで任意の例外のチェックを追加していない

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Do everything that you need to do to get the cell. Here we will deal with only string retrieval from json array. 
    if(jsonArray count]>indexPath.row){ 
     NSArray *innerArray = [jsonArray objectAtIndex:indexPath.row]; //You got the inner array 
     if(innerArray count]>2){ 
      NSString *yourDesiredString = [innerArray objectAtIndex:2]; //You got the string. Use it now as you wish. It will be 150 for the first cell, 350 for the second and 350 for the third one as well. 
     } 

    } 

} 

:次に、あなたのcellForRowAtIndexPath方法でこれを行う必要がある

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [jsonArray count]; //Provide your json array here. 
} 

あなたは自分で何をしなければならないのですか。私は無関係のデータの場合にクラッシュを防ぐために基本的なチェックを追加しました。

これは、この文字列を各セルに設定することを前提としています。あなたの質問を誤解している、または何かを質問する必要がある場合は、コメントを残してください

2

は、すべての値は、あなたが特定の位置で必要としてい含まarrResultこのコードでは、これを試してみてください試してみました。

NSArray *array = jsonArray; 
NSMutableArray *arrResult = [[NSMutableArray alloc]init]; 

for (NSArray *newArray in array) { 
     [arrResult addObject:[newArray objectAtIndex:2]]; 

} 
+0

なぜあなたは既にテーブルビューを行っているときに、配列を繰り返し処理するのですか? – NSNoob

+0

は、特定の位置ですべての値を一度に必要とするためです。 –

+0

TableViewでも取得できます。 TableViewはIndexPath.row上で動作し、インデックスとして使用します。 – NSNoob