This is for a blog-type app, in which the blog post is shown and below you can read the several comments to that post.
| Post Title Here
|
| Post text here lorem ipsum
| dolor bla bla bla
|
| ---------------------------------------------
| Comment 1
| ---------------------------------------------
| Comment 2
| ---------------------------------------------
| Comment 3
| ---------------------------------------------
| etc
My JSON feed looks like this:
...
{
"post_id": "1463",
"post_title": null,
"post_text": "dffsdjdflkjklk dlfkjsdlfkj",
"comment": [
{
"comment_id": "2162",
"comment_text": "yuiyuiiopiop",
},
{
"comment_id": "2163",
"comment_text": "tyutyutyutyuu",
},
{
"comment_id": "2164",
"comment_text": "sdfsertertr",
},
]
},
...
And this is how I read it
NSDictionary *post = self.detailItem;
NSArray *commentThread = [post objectForKey:@"comment"];
NSUInteger commentCount = [commentThread count];
for (int i = 0; i < commentCount; i++) {
NSDictionary *comment = [commentThread objectAtIndex:i];
NSString *commentText = [comment objectForKey:@"comment_text"];
commentTextLabel.text = commentText;
}
In my storyboard I have one UILabel that is wired to commentTextLabel.
With the above approach only the last comment shows in my view. I expected the UILabel to be generated i times but that does not seem to be the case.
How should I do to get multiple UILabels created, one for each comment, so they end up stacked as I show in the top of this post?
Any kind of help, pointers or advice is greatly appreciated.
Since you have one UILabel hooked up in your storyboard, you are overwriting it’s content in every iteration.
What you want to do is to create a new UILabel for each comment, set it’s frame and add it as a subview to it’s parent view and iterate to the next.
For example:
origin.x, origin.y would have to be calculated each time to fit your needs, you could insert a variable to hold the last origin.y and add it’s height and a margin each time to make it look like a list.
On the other hand, the most efficient way to do this would be to use a UITableView. Your JSON could be parsed into an array and each entry would be a cell. This way you take advantage of reusing cells. If you do not reuse cells and keep going with your current setup, your app’s performance could be affected if you have too many comments.
Also take into consideration that presenting labels in a TableView or ScrollView isn’t very effective framerate-wise. The alpha-blending will cut into your performance. I suggest you also google about tweetie’s fast performing tableviews or check this post
atebits Twitter fast scrolling