Setting a cell's text with multiple JSON objects

旧巷老猫 提交于 2019-12-01 14:20:19

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];

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";

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!