在iphone中循环异步

时间:2013-08-27 09:28:02

标签: iphone objective-c ipad ios5 ios6

for循环看起来像这样,我在视图中写入了加载,因此加载此页面需要更多时间。

for (int i=3; i<[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]count]; i++)
{


    if (i%3==0)
    {
        x=0;
        y++;
    }

    view=[[UIView alloc]initWithFrame:CGRectMake((x*250)+5, (y*404)+6, 244, 400)];
    [view setBackgroundColor:[UIColor whiteColor]];
    view.layer.borderColor=[[UIColor whiteColor]CGColor];
    view.layer.borderWidth=1.0;
    view.layer.cornerRadius = 5;
    view.layer.masksToBounds = YES;
    [scroller addSubview:view];

titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 230, 20)];
    [titlelabel setText:[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"text"]];
    [titlelabel setNumberOfLines:0];
    titlelabel.font=[UIFont boldSystemFontOfSize:15.0f];




    [titlelabel setBackgroundColor:[UIColor clearColor]];
    [titlelabel sizeToFit];
    [view addSubview:titlelabel];

    datelabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 62, 190, 20)];
    [datelabel setText:[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"text"]];
    [datelabel setNumberOfLines:0];
    datelabel.font=[UIFont fontWithName:@"arial" size:12.0f];
    [datelabel setBackgroundColor:[UIColor clearColor]];
    [datelabel sizeToFit];
    [view addSubview:datelabel];

    NSData *data=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"image"]objectForKey:@"text"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]];
    NSLog(@"data= %@",data);
    if (data==NULL ||[[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"image"]objectForKey:@"text"] isEqualToString:@""])
    {
        textview=[[UITextView alloc]initWithFrame:CGRectMake(2,80, 238, 386)];
        [textview setText:[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"text"]];
        [textview setFont:[UIFont fontWithName:@"ArialMT" size:14]];
        [textview setDelegate:self];
        [view addSubview:textview];
    }
    else
    {
        imageview=[[UIImageView alloc]initWithFrame:CGRectMake(7, 80, 230, 150)];
        [imageview setImage:[UIImage imageWithData:data]];
        [view addSubview:imageview];

        textview=[[UITextView alloc]initWithFrame:CGRectMake(5, 240, 238, 200)];
        [textview setText:[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"text"]];
        [textview setFont:[UIFont fontWithName:@"ArialMT" size:14]];
        [textview setDelegate:self];
        [view addSubview:textview];
    }

}

这里wat问我的问题是每次从服务器获取的图像所以这会变慢,请建议如何使它像延迟加载一样....

提前致谢

1 个答案:

答案 0 :(得分:0)

在我看来,UICollectionView应该是显示这种数据的更好选择, 但它与ios5不兼容。

如果您需要兼容ios 5,请查看答案:https://stackoverflow.com/a/16039194/2707614

最后,要回答您的问题,您可以尝试使用Grand Central Dispatch  正如here

所解释的那样

看起来应该是这样的(我没有计算机来测试):

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

 dispatch_async(queue, ^{
for (int i=3; i<[[[self rssParser]rssItems]count]; i++)
    // for (int i=3; i<[titlearray count]; i++)
{
    if (i%3==0)
    {
        x=0;
        y++;
    }

    view=[[UIView alloc]initWithFrame:CGRectMake((x*250)+10, (y*306)+105, 244, 300)];
    [view setBackgroundColor:[UIColor whiteColor]];
    view.layer.borderColor=[[UIColor whiteColor]CGColor];
    view.layer.borderWidth=1.0;
    view.layer.cornerRadius = 5;
    view.layer.masksToBounds = YES;



    titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 230, 20)];
    [titlelabel setText:[[[[self rssParser]rssItems]objectAtIndex:i-1]title]];
    [titlelabel setNumberOfLines:0];
    titlelabel.font=[UIFont boldSystemFontOfSize:14.0f];

    [titlelabel setBackgroundColor:[UIColor clearColor]];
    [titlelabel sizeToFit];


    datelabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 62, 190, 20)];
    [datelabel setText:[[[[self rssParser]rssItems]objectAtIndex:i-1]pubDate]];
    [datelabel setNumberOfLines:0];
    datelabel.font=[UIFont fontWithName:@"arial" size:10.0f];
    [datelabel setBackgroundColor:[UIColor clearColor]];
    [datelabel sizeToFit];

    x++;

    dispatch_sync(dispatch_get_main_queue(), ^{
        [scroller addSubview:view];
        [view addSubview:titlelabel];
        [view addSubview:datelabel];
    });

}
});
相关问题