简体   繁体   English

JSON数据中的UITableView部分

[英]UITableView sections from JSON data

I have JSON data like this: 我有这样的JSON数据:

[{"id":"3","name":"jason"},{"id":"4","name":"karen"}]

I want to build a table view with a section for each pair [id, name]. 我想为每个对[id,name]建立一个带有部分的表视图。 The section title should be the id value, the only cell for each section should be the name value. 节标题应为id值,每个节的唯一单元格应为名称值。

How can I parse the JSON data into an array and use [array count] in order to determine how many section have to be displayed? 如何将JSON数据解析为数组并使用[array count]来确定必须显示多少部分?

Thanks very much.. 非常感谢..

forgive my bad English! 原谅我的英语不好!

Implement the UITableViewDatasource following methods : 实现UITableViewDatasource以下方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [JSONArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    return 1;
}

To set the section's title value : 设置部分的标题值:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[JSONArray objectAtIndex:section] objectForKey:@"id"];
}

To set your cell's value implement UITableViewDelegate : 要设置单元格的值,请实现UITableViewDelegate

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.text = [[JSONArray objectAtIndex:indexPath.section] objectForKey:@"name"]; // set the cell's text here
    }
    return cell;
}

For reference check UITableViewDataSource and UITableViewDelegate 作为参考,请检查UITableViewDataSourceUITableViewDelegate

Have a look at this project. 看看这个项目。 Its a framework to work with JSON: https://github.com/stig/json-framework/ 它是使用JSON的框架: https : //github.com/stig/json-framework/

A tutorial for this framework can be found at: http://iosdevelopertips.com/networking/iphone-json-flickr-tutorial-part-1.html 可以在以下位置找到该框架的教程: http : //iosdevelopertips.com/networking/iphone-json-flickr-tutorial-part-1.html

The framework adds a category to NSString. 该框架为NSString添加了一个类别。 With that category you can parse JSON data into a NSArray (a list of objects as for your example) or a NSDictionary (some object or structure): 使用该类别,您可以将JSON数据解析为NSArray(例如您的示例的对象列表)或NSDictionary(某些对象或结构):

#import "JSON.h"
...
NSString jsonString = //get your JSON from somewhere
NSArray * array = [jsonString JSONValue];

I hope I could give you an impression, what to do in order to achieve your goal. 希望我能给您留下印象,如何实现目标。 Further information is in the tutorial of the JSON project or in the tutorial mentioned. 有关更多信息,请参见JSON项目的教程或上述教程。

How to build a table from your JSON Array is in the answer: UITableView sections from JSON data 答案是:如何从JSON数据构建UITableView部分,如何从JSON数组构建表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM