从NSDictionary填充tableview [英] Populate tableview from NSDictionary

查看:111
本文介绍了从NSDictionary填充tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an NSDictionary received from a json request that looks like this:

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] 等。

I want to display it in a TableView (CellforRowAtIndexPath), so I obtain the data as NSArray. The method seems inefficient as each key Id1, lat, long, etc is created as an NSArray so that I can display them each with: [self.data1 objectAtIndex:indexPath.row]; [self.data2 objectAtIndex:indexPath.row], etc.

如果不创建和使用4个NSArrays,我怎样才能实现同样的目标?我可以使用吗?单个NSArray或存储数据的NSMutableDictionary?

How can I achieve the same thing without creating and using 4 NSArrays? Can I use a single NSArray or the NSMutableDictionary which stores the data?

更新:

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

When the TableView loads, it is initially empty but I have a button on that same VC that loads a modal view of a form. When I load the form and then dismiss it returning to the TableView, the data is LOADED! Could you suggest what I am missing?

推荐答案

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

Yes you can use a single array. The trick is to create an array with each array entry holding a dictionary. Then you query the array to populate your tableview.

例如:如果你的数组是一个名为 tableData 的属性,那么你有自定义的tableview单元名为 CustomCell 然后你的代码可能如下所示:

E.g.: If your array is a property called tableData and you have custom tableview cell called CustomCell then your code might look something like the following:

- (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的代码类似于以下内容:

Similarly, if you have multiple sections in your tableview then you will construct an array of arrays, with each sub-array containing the dictionaries for that section. The code to populate the tableview would look something similar to the following:

- (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。

TL;DR; Take your dictionaries created from your JSON data and put them in an array. Then query the array to populate the tableview.

这篇关于从NSDictionary填充tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆