我已经尝试了几天,以找出如何将此JSON解析为一个分段的UITable,但是我没有成功,我只能弄清楚如何获取该节的名称,但未能获得每个节的行数和数据每个部分的每一行。
由于运输组可能会不时变化,并且名称可能会发生变化,所以我想我需要使用allKeys来查找每个部分的标题1st。
请帮忙,为我指出正确的方向,以提取部分UITable的数据,谢谢。
{
"transport" : {
"public" : [
{
"transport_id" : "2",
"transport_name" : "Ferry"
},
{
"transport_id" : "3",
"transport_name" : "Bus"
},
{
"transport_id" : "4",
"transport_name" : "Taxi"
},
{
"transport_id" : "5",
"transport_name" : "Tram"
}
],
"Private" : [
{
"transport_id" : "11",
"transport_name" : "Bicycle"
},
{
"transport_id" : "12",
"transport_name" : "Private Car"
}
],
"Misc" : [
{
"transport_id" : "6",
"transport_name" : "By Foot"
},
{
"transport_id" : "7",
"transport_name" : "Helicopter"
},
{
"transport_id" : "8",
"transport_name" : "Yatch"
}
]
}
}
NSDictionary *results = [jsonString JSONValue];
NSDictionary *all = [results objectForKey:@"transport"];
NSArray *allKeys = [all allKeys];
NSArray *transports = [results objectForKey:@"transport"];
for (NSDictionary *transport in transports)
{
[transportSectionTitle addObject:(transport)];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [transportSectionTitle count];
}
最佳答案
最简单的解释方法是使用all
词典作为您的数据源。
NSDictionary *results = [jsonString JSONValue];
NSDictionary *all = [results objectForKey:@"transport"];
// self.datasource would be a NSDictionary retained property
self.datasource = all;
然后获取部分数量,您可以执行以下操作:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.datasource count]; // You can use count on a NSDictionary
}
要获取各部分的标题:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *title = [[self.datasource allKeys] objectAtIndex:section];
return title;
}
要获取每个节中的行数:
- (NSInteger)tableView:(UITableView *)favTableView numberOfRowsInSection:(NSInteger)section {
// Get the all the transports
NSArray *allTransports = [self.datasource allValues];
// Get the array of transports for the wanted section
NSArray *sectionTransports = [allTransports objectAtIndex:section];
return [sectionTransports count];
}
然后获取行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Get the all the transports
NSArray *allTransports = [self.datasource allValues];
// Get the array of transports for the wanted section
NSArray *sectionTransports = [allTransports objectAtIndex:indexPath.section];
// Then get the transport for the row
NSDictionary *transport = [sectionTransports objectAtIndex:indexPath.row];
// Now you can get the name and id of the transport
NSString *tansportName = [transport objectForKey:@"transport_name"];
NSString *transportId = [transport objectForKey:@"transport_id"];
NSString *transportDescription = [NSString stringWithFormat:@"%@ - %@",transportId, transportName];
cell.textLabel.text = transportDescription;
return cell;
}
无论如何,这就是要旨。
您可能想将
allKeys
和allValues
数组存储为类属性,而不是必须在所有tableview的委托和数据源方法中进行遍历,但是现在您应该拥有所有信息来构建您的表。希望这可以帮助 :)
关于iphone - 分段的UITable和JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9030530/