我是 Objective C 和 IOS 编程新手,所以请耐心等待。
我从我的服务器获取一个 JSON 字符串,我用它来向我的 View 添加图像或按钮,我的 View 是 UIScrollView,
下面的代码工作得很好,并且完全符合我的要求,但我只能在循环完成后看到所有按钮,这需要一些时间。
我期待的是看到每个按钮都被一一添加到 View 中。有没有更好的方法来实现这一点,或者这是我所描述的预期行为。
谢谢
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);
}
最佳答案
您需要在后台线程中运行图像加载过程,这应该可以解决它。我将解释 - 图像加载会停止 UI,而循环实际上仍在运行,添加带有图像的按钮,但在加载所有图像之前它们不会显示。
关于iphone - 以编程方式将 UIButtons 添加到 UIScrollView 在循环完成之前不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9139543/