从iOS中的多维数组迭代

ios

如何在iOS中从这个多维数组进行迭代?此数据来自JSON

(
        (
        "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"
    )
)

我需要在UITableView中使用索引3处的对象,例如= 150,350,350。

我已经试过了

NSArray *array = jsonArray;

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

这很简单。让TableView为您迭代数组。您的tableView已经在迭代,不要通过自己遍历数组来增加开销。如果这样做,只会浪费处理能力。

首先,修改您的numberOfRowsInSection委托方法,如下所示:

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

然后,您必须在您的cellForRowAtIndexPath方法中执行此操作

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

    }

}

我没有在这里添加任何异常检查,这就是您自己要做的事情我添加了基本检查以防止在数据不相关的情况下崩溃,您应该对此进行扩展。

这是基于您希望将此字符串设置到每个单元格的假设。如果我误解了您的问题或需要提出问题,请发表评论

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章