我正在尝试将NSDictionary数据放入表格视图中。但是我在访问值时遇到了问题。
字典看起来像这样:
multimedia = {
uploads = {
upload = (
{
FileName = {
text = "\n 1-FB5A9792.MOV";
};
status = {
text = "\n 2";
};
Event = {
text = "\n Test";
};
Date = {
text = "\n 9/7/2016 9:06:21 AM";
};
Publicacion = {
text = "\n Example";
};
Seccion = {
text = "\n Sistemas";
};
id = {
text = "\n \n \n 993";
};
text = "\n ";
},
{
FileName = {
text = "\n 2-FB5A9793.MOV";
};
status = {
text = "\n 2";
};
Event = {
text = "\n Test";
};
Date = {
text = "\n 9/7/2016 9:06:21 AM";
};
Publicacion = {
text = "\n Example";
};
Seccion = {
text = "\n Sistemas";
};
id = {
text = "\n \n 994";
};
text = "\n ";
},
{
FileName = {
text = "\n 1-IMG_0006.MOV";
};
status = {
text = "\n 2";
};
Event = {
text = "\n Highest";
};
Date = {
text = "\n 9/7/2016 4:56:37 PM";
};
Publicacion = {
text = "\n Example";
};
Seccion = {
text = "\n Sistemas";
};
id = {
text = "\n \n 995";
};
text = "\n ";
},...
我使用XMLReader将XML转换为Dictionary,然后将上述数据传递给tableData
tableData = [jsonDictionary allKeys];
表
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
NSString *key = [tableData objectAtIndex:indexPath.row];
NSDictionary *dictionary = [jsonDictionary objectForKey:key];
// get values from the dictionary and set the values for the displayed cell ...
return cell;
}
我试图为每个FileName和状态值放置一个单元格,在这种情况下为每个FileName放置3个单元格。 但我不知道该怎么做或得到FileName的数量。
这是XML
<multimedia>
<uploads>
<upload>
<id>1136</id>
<Date>9/9/2016 11:01:58 AM</Date>
<FileName>IMG_0510.MOV</FileName>
<status>2</status>
<Publicacion>Example</Publicacion>
<Seccion>Sistemas</Seccion>
<Event>Q</Event>
</upload>
</uploads>
</multimedia>
答案 0 :(得分:1)
在我看来,计数应该是1.
原始jsonDictionary只有一个项目。您应该更深入地进行上传,然后调用计数。
键@"upload"
包含数组。因此,jsonDictionary[@"multimedia"][@"uploads"][@"upload"]
应返回array
。如果将其转换为NSDictionary
,则会返回错误的结果,如果您尝试获取密钥File name
的值,则会得到错误的结果。
字典的主要用途是您只有一个特定键值。这就是为什么如果将数组转换为File name
,只能获得一个NSDictionary
。
所以:
NSArray * array = _xmlDictionary[@"multimedia"][@"uploads"][@"upload"];
NSDictionary * onUpload = array[i];
NSDictionary * fileName = array[i][@"FileName"];
NSString * fileNameString = array[i][@"FileName"][@"text"];
所以在你的情况下:
tableData = jsonDictionary[@"multimedia"][@"uploads"][@"upload"];
然后
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
NSDictionary *upload = [tableData objectAtIndex:indexPath.row];
NSString *fileName = upload[@"FileName"][@"text"];
NSString *status = upload[@"status"][@"text"];
NSString *event = upload[@"event"][@"text"];
...
return cell;
}
答案 1 :(得分:0)
获取以下数据:NSMutableArray *tableData = dict[@"multimedia"][@"uploads"][@"upload"];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
NSDictionary *upload = [tableData objectAtIndex:indexPath.row];
// get values from the dictionary and set the values for the displayed cell ...
NSString *event = upload[@"Event"][@"text"];
event = [event stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.labelEvent.text = event;
NSString *date = upload[@"Date"][@"text"];
date = [date stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.labelDate.text = date;
return cell;
}
答案 2 :(得分:0)
我已将JSON放在本地JSON文件中并对其进行解析以显示结果。
所以你可以在获取字典后按照代码
所以你应该从这一行开始
NSArray *json = jsonTemp[@"multimedia"][@"uploads"][@"upload"];
jsonTemp
包含您词典的所有数据。
在我只显示文件名的代码中,您可以使用其他键来相应地显示其他数据。
这是我的代码:
- (void)loadJsonData
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:filePath];
NSDictionary *jsonTemp = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
NSArray *json = jsonTemp[@"multimedia"][@"uploads"][@"upload"];
for (NSDictionary *dictTemp in json) {
[arrEvents addObject:dictTemp];
}
[tblView reloadData];
}
#pragma mark - TableView Delegate -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrEvents count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 49;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ListingIdentifier";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
NSDictionary *dictTemp = [arrEvents objectAtIndex:indexPath.row];
NSString *fileName = dictTemp[@"FileName"][@"text"];
cell.textLabel.text = [NSString stringWithFormat:@"File Name : %@", fileName];
cell.backgroundColor = CLEAR_COLOR;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
希望有所帮助
快乐的编码......