Setting a cell's text with multiple JSON objects

守給你的承諾、 提交于 2019-12-01 12:34:43

问题


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.


回答1:


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



回答2:


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



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


来源:https://stackoverflow.com/questions/16222531/setting-a-cells-text-with-multiple-json-objects

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