开发者

NSArray in a Label

开发者 https://www.devze.com 2023-01-13 16:57 出处:网络
I would like to display the contents of the NSMutable array in a label. I have the following code that displays only the last object. What would be the 开发者_JS百科method to display ALL the objects

I would like to display the contents of the NSMutable array in a label.

I have the following code that displays only the last object. What would be the 开发者_JS百科method to display ALL the objects in the array (in this case "values")?

self.lblMessage.text = [NSString stringWithFormat:@"%@\n%@", 
self.lblMessage.text, [values objectAtIndex:[values count]-1]];


Following code should do what you need:

label.numberOfLines = 0; // to make sure your label is able to display multiple lines
label.text = [values componentsJoinedByString:@"\n"]; //insert separator symbol you need in place of "\n"


To get all values in an NSArray joined by a delimiter like ", " use [values componentsJoinedByString:@", "]. The delimiter can of course be "\n" if you like, but you need to make sure your label or textfield supports multiple lines.

Also, your [values objectAtIndex:[values count]-1] can be better expressed as [values lastObject]. :)


Normally a label is only to show one line of text. And you use \n in your code. So there are multiple lines. Delete The \n in your code or try tu use a UITextView. ;-) There's also a way to force UILabel to display multiple lines, but I don't know that one on the go...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号