iphone - 以编程方式将 UIButtons 添加到 UIScrollView 在循环完成之前不会显示

标签 iphone objective-c ios

我是 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/

相关文章:

iphone - dequeueReusableCellWithIdentifier :identifier not picking up a loadNibNamed cell

iphone - 我可以安全地使用 CGFloat 来保存 Float64 或 Float32 值吗?

objective-c - 为什么 NSLog 显示错误的 int 和 char?

ios - XML 解析以获取链接,在 Objective-C 中

android - jQuery 移动按钮按下事件处理缓慢

ios - UITableview 从上一个 View 返回时更改 Y 位置

ios - 更改 UIAlertView 中 UITextField 的字体大小

ios - 显示 iOS 表情符号键盘

objective-c - Objective C - 如何创建接口(interface)?

ios - 主视图未使用详细 View 中的委托(delegate)进行更新