user1446686
user1446686

Reputation: 23

Concatenate two strings in UITableView's detailedTextLabel

Hi I have this line of code.

cell.detailTextLabel.text = [[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"city"];

I want to use two objectForKeys. I have a "city" & "state" key I would like them to put together in that detailedTextLabel. How would I do that?

Upvotes: 0

Views: 668

Answers (5)

Mani
Mani

Reputation: 1841

NSString *city = [[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"city"];
NSString *state =[[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"state"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"city is : %@  state is: %@", city, state];

Upvotes: 1

Kalzem
Kalzem

Reputation: 7501

Did you try something like this ?

NSString *city = [[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"city"];
NSString *state = [[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"state"];
cell.detailTextLabel.text = [[NSString alloc]initWithFormat:@"%@ %@",city,state];

Upvotes: 0

Apurv
Apurv

Reputation: 17186

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@",
                             [[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"city"], 
                             [[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"state"]];

Upvotes: 5

Nuzhat Zari
Nuzhat Zari

Reputation: 3408

Try this:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@",[[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"city"],[[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"state"]];

Upvotes: 1

Mike Weller
Mike Weller

Reputation: 45598

You can do something like this:

NSString *city = [[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"city"];
NSString *state = [[self->jsonArray objectAtIndex:indexPath.row] objectForKey:@"state"];
NSString *combined = [NSString stringWithFormat:@"%@ (%@)", city, state];
cell.detailTextLabel.text = combined;

Upvotes: 1

Related Questions