Reputation: 93
I've got a simple web service in passing through some data into a table view and it's working fine.
This is the code I currently have:
[[cell detailTextLabel] setText:[item objectForKey:@"Ball 1"]];
As I said, this works perfectly and displays with a ball number, e.g. "1", taken from the web service.
What I want to do is have it display, e.g. "1 & 3".
Looking at some other code I thought it might have been:
[[cell detailTextLabel] setText:[item objectForKey:@"Ball 1"]item objectForKey:@"Ball 2"]];
I'm totally new to Objective-C, and I'm doing a few tutorials and I'm trying to expand on them a bit. It's been a bit difficult to search for answers on some of the stuff because I'm unsure of the terminology.
Upvotes: 0
Views: 134
Reputation: 64002
Your cell's text
property needs to be set with one single string, but you have two strings that you want to use. You need to combine these strings.
The string object in Cocoa Touch is NSString
, and it has a few methods that could work for you. The best one in this case is stringWithFormat:
. This takes a "format string", which describes the output you'd like, and other arguments that are inserted into the result.
Your format string in this case will be: @"%@ & %@"
. That is a string literal with two format specifiers -- the two %@
s. Each specifier indicates the type of the following arguments that should be inserted into the string -- in this case, any kind of object (as opposed to int
or float
). The &
in the string has no special meaning -- it will appear in the result as it is.
To get the result you call stringWithFormat:
, passing your format string and the two objects you want inserted:
NSString * result = [NSString stringWithFormat:@"%@ & %@", [item objectForKey:@"Ball 1"], [item objectForKey:@"Ball 2"]];
Now you have a single string, and can assign it to your cell's text
:
[[cell detailTextLabel] setText:result];
Upvotes: 1
Reputation: 3045
I'm not sure if you want to set the text from the strings in item or if you simply want to print "1 & 3"
Let's pretend it's the first. You can set the text by
cell.detailTextLabel.text = [[NSString stringWithFormat:@"%@ %@", [item objectForKey:@"Ball 1"], [item objectForKey:@"Ball 3"]];
NSString stringWithFormat: will construct a string and replace %@ with the NSObject you provide, becareful because if you put an int or a float it will crash. You need to use the proper identifiers to write properly, %d for int and %f for float for example.
If you simply want to write "1 & 3" into text it's this.
cell.detailTextLabel.text = @"1 & 3";
Upvotes: 1
Reputation: 37729
Use string concatenation, e.g stringWithFormat
:
NSString *concated = [NSString stringWithFormat:@"%@ & %@", [item objectForKey:@"Ball 1"], [item objectForKey:@"Ball 3"]];
and now set this string to label:
[[cell detailTextLabel] setText:concated];
Upvotes: 2