2

我从 json 请求中收到了一个 NSDictionary,如下所示:

RESULT : (
    {
    Id1 = 138;
    lat = "45.5292910";
    long = "-73.6241500";
    order = "2343YY3"

},
    {
    Id1 = 137;
    lat = "45.5292910";
    long = "-73.6241500";
    order = "2343YY3"

}, etc.

我想在 TableView (CellforRowAtIndexPath) 中显示它,所以我将数据作为 NSArray 获取。该方法似乎效率低下,因为每个键Id1, lat,long等都是作为 NSArray 创建的,因此我可以使用:[self.data1 objectAtIndex:indexPath.row]; [self.data2 objectAtIndex:indexPath.row]等显示它们。

如何在不创建和使用 4 个 NSArray 的情况下实现相同的目标?我可以使用存储数据的单个 NSArray 或 NSMutableDictionary 吗?

更新:

当 TableView 加载时,它最初是空的,但我在同一个 VC 上有一个按钮,用于加载表单的模式视图。当我加载表单然后将其关闭返回 TableView 时,数据已加载!你能建议我错过什么吗?

4

2 回答 2

3

是的,您可以使用单个数组。诀窍是创建一个数组,其中每个数组条目都包含一个字典。然后你查询数组来填充你的tableview。

例如:如果您的数组是一个名为的属性tableData,并且您调用了自定义 tableview 单元格,CustomCell那么您的代码可能如下所示:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // Configure the cell...
    cell.latitude.text = [[self.tableData objectAtIndex:indexPath.row] objectForKey: @"lat"];
    cell.longitude.text = [[self.tableData objectAtIndex:indexPath.row] objectForKey:@"long"];
    // continue configuration etc..
    return cell;
}

同样,如果您的表格视图中有多个部分,那么您将构建一个数组数组,每个子数组都包含该部分的字典。填充 tableview 的代码类似于以下内容:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return [self.tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [[self.tableData objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // Configure the cell...
    cell.latitude.text = [[[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey: @"lat"];
    cell.longitude.text = [[[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"long"];
    // continue configuration etc..
    return cell;
}

TL;博士; 获取从 JSON 数据创建的字典并将它们放入一个数组中。然后查询数组以填充 tableview。

于 2012-11-02T03:13:28.173 回答
0

您可以执行以下操作:

// main_data = Store your JSON array as "array of dictionaries"

然后在cellForRowAtIndexPath做如下:

NSDictionary *obj = [main_data objectAtIndex: indexPath.row];
// Access values as follows:
[obj objectForKey: @"Id1"]
[obj objectForKey: @"lat"]
...
...
于 2012-11-02T02:37:44.357 回答