Reputation: 370
I'm trying to put NSDictionary data into a table view. But I'm having problem accesing values.
The dictionary looks like this:
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 ";
},...
I´m using XMLReader to convert an XML to Dictionary, then pass the above data to tableData
tableData = [jsonDictionary allKeys];
Table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
Creating cell
- (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;
}
I´m trying to put a cell for each FileName and status values, in this case 3 cells for each FileName. But I'm not sure how to do that or get the count of FileName's.
This is the 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>
Upvotes: 0
Views: 222
Reputation: 8730
It looks to me that count should be 1.
Original jsonDictionary has only one item. You should go deeper to the upload and then call count.
Key @"upload"
holds array inside. So, jsonDictionary[@"multimedia"][@"uploads"][@"upload"]
should return an array
. If you cast it into NSDictionary
it will return wrong results, and you will get wrong results if you will try to get value for key File name
.
The main purpose of dictionaries are that you have only one value for specific key. This is why you only get one File name
, if you cast array into NSDictionary
.
So :
NSArray * array = _xmlDictionary[@"multimedia"][@"uploads"][@"upload"];
NSDictionary * onUpload = array[i];
NSDictionary * fileName = array[i][@"FileName"];
NSString * fileNameString = array[i][@"FileName"][@"text"];
So in your case :
tableData = jsonDictionary[@"multimedia"][@"uploads"][@"upload"];
and then
- (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;
}
Upvotes: 1
Reputation: 7893
Get data like this: 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;
}
Upvotes: 0
Reputation: 4163
I have taken your JSON in a local JSON file and parsed that to display the result.
So you can follow the code after getting the dictionary,
So the line you should start from this line
NSArray *json = jsonTemp[@"multimedia"][@"uploads"][@"upload"];
jsonTemp
containing all data of your dictionary.
Here in the code i am showing only File name, you can use other key to display the other data accordingly.
Here is My Code :
- (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;
}
Hope it helps
Happy coding ...
Upvotes: 0