Reputation: 3996
I am new to Objective C and IOS programming so please be patient with me.
I am getting a JSON string from my server which I am using to add images or buttons to my view my view is a UIScrollView,
The below code works great and does exactly what I want but I only see all the buttons once the loop is completed which takes some time.
What I was expecting is to see each button being added to the view one by one. Is there a better way to implement this and or is this the expected behavior for this as I described.
Thank You
for (NSDictionary *num in jsonOBJ) {
fullURL = [urlWithOut stringByAppendingString:[num objectForKey:@"src"]];
img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:fullURL]]];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(3+i*h, 3+i*w,76, 76);
button.layer.borderWidth = 1.0;
button.layer.borderColor = [UIColor redColor].CGColor;
[button addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
[self.scroll addSubview:button];
[button setImage:img forState:UIControlStateNormal];
[img release];
NSString *string = @"";
NSString *realstring = [string stringByAppendingString: [num objectForKey:@"id"]];
button.tag = [realstring intValue];
self.scroll.contentSize = CGSizeMake(320,total);
}
Upvotes: 0
Views: 622
Reputation: 8090
You need to run image loading process in background thread, that should solve it. I'll explain - image loading stops the UI while loop actually is still running, adding buttons with images, but they won't display until all images are loaded.
Upvotes: 1