【问题标题】:Populate tableview from NSDictionary从 NSDictionary 填充 tableview
【发布时间】:2012-11-02 02:32:13
【问题描述】:

我从 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 的形式获取数据。该方法似乎效率低下,因为每个键 Id1latlong 等都被创建为一个 NSArray,以便我可以使用:[self.data1 objectAtIndex:indexPath.row];[self.data2 objectAtIndex:indexPath.row] 等来显示它们。

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

更新:

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

【问题讨论】:

    标签: ios uitableview nsarray nsdictionary


    【解决方案1】:

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

    例如:如果您的数组是一个名为 tableData 的属性,并且您有一个名为 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 中有多个部分,那么您将构建一个数组数组,每个子数组包含该部分的字典。填充 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;DR;获取从 JSON 数据创建的字典并将它们放入一个数组中。然后查询数组以填充 tableview。

    【讨论】:

    • 我在 tableView 中遇到问题:numberOfRowsInSection:当我执行 [self.results count] 因为它总是返回 0 时,你知道为什么,因为这很奇怪吗?当我在 self.results = [myDictionary valueForKey:@"result"]; 之后 NSLog 计数时结果是我的 NSArray 它给了我正确的数字 25,但在那个方法中它是 0。该表在那时被冻结。另外,我将 tableviewcell 子类化,因为我有两个类似的设计,另一个运行良好。感谢您的帮助,我正在寻找您的建议。
    • 如果 [self.results count] 返回零(在任何时候),那么数组中没有值(在那个时候)。加载数组的代码可能有错误,或者您可能正在覆盖数组。首先,检查该属性是否声明为(strong, copy)。其次,尝试在调试器中单步执行代码,检查每一步的数据。
    • 非常感谢!我取得了一些进展,但我还有一个难题。如果你认为我应该提出一个新问题,请告诉我。当 tableview 加载时,它最初是空的,但我在同一个 VC 上有一个按钮,用于加载表单的模式视图。当我加载表单然后将其关闭返回 tableview 时,数据已加载!你能建议我缺少什么吗?
    • @Paulo:听起来绝对是个新问题。将更多详细信息发布到 StackOverflow,以便更多人可以查看并提供帮助。但听起来您的数组self.results 在首次加载时不可用。确保在 ViewDidLoad 而不是 ViewDidAppear 之类的地方创建数组(这在视图生命周期中可能为时已晚)。
    【解决方案2】:

    你可以这样做:

    // 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-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    相关资源
    最近更新 更多