Reputation: 49
i have looked everywhere for this, but i am unable to find the exact answer... all have slight variations.
Anyway, i call a json page that returns the following (from the NSLog):
{
messages = {
1 = {
Body = "This is the body of message 1";
Title = "Message 1";
};
2 = {
Body = "This is the body of message 2";
Title = "Message 2";
};
};
}
I then save the data into a NSDictionary (called messageArray). (the array is a NSMutableArray)
then i do:
// Customize the appearance of table view cells.
- (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];
}
//put rowsarray into dictionary
NSDictionary *dictionary = [messageArray objectAtIndex:indexPath.section];
//new dictionary into array
NSArray *messages = [dictionary objectForKey:@"messages"];
NSLog(@"the message array = %@",messages );
//this fails
cell.textLabel.text = [messages objectAtIndex:indexPath.row];
return cell;
the returned NSLog (so i am assuming my json array is working correctly):
the message array = {
1 = {
Body = "This is the body of message 1";
Title = "Message 1";
};
2 = {
Body = "This is the body of message 2";
Title = "Message 2";
};
}
I understand that i am not labelling the textlabels.text correctly, but i am not sure how to go about looping through the "messages" array, to display all the "Title" values from the array, to display on my UITableView list.
i am certain i am missing something so simple... but it has eluded me until now. Any links welcome... i will keep searching myself....
Upvotes: 0
Views: 1281
Reputation: 12787
NSArray *messages = [dictionary objectForKey:@"messages"];
what is the need of this line if you get your dictionary here
NSDictionary *dictionary = [messageArray objectAtIndex:indexPath.section];
now, you know what are the keys for attaching data to dictionary.
then simply
cell.textLabel.text = [dictionary valueForKey:@"Title"];
cell.detailTextLabel.text= [dictionary valueForKey:@"Body"];
Upvotes: 1
Reputation: 49
i have "semi" sorted the issue. I have removed the indexes from the json data so now it looks like:
messages = (
{
Body = "This is the body of message 1";
Title = "Message 1";
},
{
Body = "This is the body of message 2";
Title = "Message 2";
}
);
And have used this for the populating of the table:
- (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];
}
//put rowsarray into dictionary
NSDictionary *dictionary = [messageArray objectAtIndex:indexPath.section];
NSLog(@"dictionary = %@", dictionary );
//new dictionary into array
NSArray *messages = [dictionary objectForKey:@"messages"];
//NSArray *message=[[messages allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"array messages = %@", messages );
cell.textLabel.text = [[messages objectAtIndex:indexPath.row] objectForKey:@"Title"];
return cell;
}
i hope this helps someone.
Upvotes: 0
Reputation: 13247
cell.textLabel.text = [[messages objectAtIndex:indexPath.row] objectForKey:@"Title"];
cell.detailTextLabel.detailLabel = [[messages objectAtIndex:indexPath.row] objectForKey:@"Body"];
Upvotes: 0
Reputation: 15588
This call
cell.textLabel.text = [messages objectAtIndex:indexPath.row];
looks like it is returning a dictionary, and you are trying to stuff it into a test field. I would confirm that this is a dictionary first, something like:
NSLog(@"%@", [messages objectAtIndex:indexPath.row])
If that is not a string, that may be your problem.
To get the string you want, you might do something like:
[[messages objectAtIndex:indexPath.row] valueForKey:@"Title"]
Upvotes: 0