ios - 在 For 循环的每次迭代中创建一个新数组

标签 ios arrays for-loop ios7

过了这么久,我仍在尝试创建我的第一个 iOS 应用程序,并且我再次面临另一个问题 =(

基本上,我从网络上提取了 JSON 数据,并且得到了嵌套数组。我可以毫无问题地迭代数组中的所有对象(包括父数组)并将它们放入 UITableView 中。问题是,我正在尝试为 table 做一些动态的事情。我能够为表创建正确数量的部分。为了让您轻松可视化我想要做的事情,这里是 JSON 数据:

{"filter_group":[{"filter_group_id":"1","name":"Location","filter":[{"filter_id":"2","name":"Central"},{"filter_id":"8","name":"East"},{"filter_id":"1","name":"North"},{"filter_id":"10","name":"Northeast"},{"filter_id":"9","name":"West"}]},{"filter_group_id":"3","name":"Price","filter":[{"filter_id":"7","name":"Free"},{"filter_id":"5","name":"$0 - $50"},{"filter_id":"6","name":"$50 - $100"},{"filter_id":"11","name":"$100 - $150"},{"filter_id":"12","name":"$150 - $200"},{"filter_id":"13","name":"Above $200"}]}]}

当您将我提供给您的困惑 block 复制到任何 JSON 查看器中时,您现在会看到,我有 2 个父数组,每个数组都有一些子数组。所以基本上,“位置”和“价格”将分为不同的部分。我已经成功地做到了这一点,但我不知道如何将他们 child 的名字放在 UITableView 的正确部分。

所以我现在的想法是迭代数组,并将子项名称放入另一个数组(这导致两个部分的名称进入 1 个数组)。我正在考虑在每次迭代中创建一个新数组:

for(int i = 0; i < [myArray count]; i++) {
//throw children of different parents into different array
   NSMutableArray* newArray[i] = (blah blah blah)
}

您现在可能已经看到了这个问题:newArray[i]。基本上,我可以接受我正在创建 newArray0、newArray1 等的事实(我需要我的应用程序是动态的)。我如何在 iOS 编程中做到这一点?

看了上面我想做的,大家对我怎么把数据放到各个section中有不同的想法,还请赐教。

非常感谢!!!我非常感谢您提供的任何帮助=D

附言我的回复可能比较慢,请见谅

最佳答案

在您的应用程序中,您可以使用来自 json 对象的数组。我制作了示例项目来说明这个过程:

在头文件(ViewController.h)中:

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) NSMutableArray *sectionTitles;
@end

在 ViewController.m 文件中:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSString *jsonString = @"{\"filter_group\":[{\"filter_group_id\":\"1\",\"name\":\"Location\",\"filter\":[{\"filter_id\":\"2\",\"name\":\"Central\"},{\"filter_id\":\"8\",\"name\":\"East\"},{\"filter_id\":\"1\",\"name\":\"North\"},{\"filter_id\":\"10\",\"name\":\"Northeast\"},{\"filter_id\":\"9\",\"name\":\"West\"}]},{\"filter_group_id\":\"3\",\"name\":\"Price\",\"filter\":[{\"filter_id\":\"7\",\"name\":\"Free\"},{\"filter_id\":\"5\",\"name\":\"$0 - $50\"},{\"filter_id\":\"6\",\"name\":\"$50 - $100\"},{\"filter_id\":\"11\",\"name\":\"$100 - $150\"},{\"filter_id\":\"12\",\"name\":\"$150 - $200\"},{\"filter_id\":\"13\",\"name\":\"Above $200\"}]}]}" ;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

    NSArray *filter_groups = [result objectForKey:@"filter_group"];
    self.data = [[NSMutableArray alloc] initWithCapacity:[filter_groups count]];
    self.sectionTitles = [[NSMutableArray alloc] initWithCapacity:[filter_groups count]];

    for (NSDictionary *filter_group in filter_groups) {
        NSArray *filters = [filter_group objectForKey:@"filter"];
        [self.data addObject:filters];
        [self.sectionTitles addObject:[filter_group objectForKey:@"name"]];
    }
    NSLog(@"%@", self.data);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.sectionTitles[section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.data count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sectionArray = self.data[section];
    return [sectionArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testCell"];
    }
    NSArray *sectionArray = self.data[indexPath.section];
    NSDictionary *filter = sectionArray[indexPath.row];
    cell.textLabel.text = [filter objectForKey:@"name"];
    return cell;
}

@end

如果您对编码有相同的问题,请随时提问。您可以从 https://gist.github.com/MaciejGad/8452791 获取代码

关于ios - 在 For 循环的每次迭代中创建一个新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21157524/

相关文章:

JAVA - for 循环中的 length()

ios - 发布请求不起作用但在浏览器中工作正常?

C - 过滤整数数组的最有效方法

java - 获取二进制数组中最大和最小的一组

arrays - 将静态数组转换为 Delphi 中的指针?

java - 从列表中删除第二项会导致 REST API 崩溃

javascript - 为什么 addEventListener 在事件发生之前触发?

iOS- UIWebView 和下载的代码

ios - 如何以编程方式以模态方式呈现 View Controller ?

ios - swift: nsurlsession 下载文件