Asynchronus for loop in iphone

后端 未结 1 1887
逝去的感伤
逝去的感伤 2021-01-28 10:12

the for loop looks like this , which i have written in view did load, so it takes more time to load this page.

for (int i=3; i<[[[[dataDict objectForKey:@\"rs         


        
1条回答
  •  孤城傲影
    2021-01-28 10:22

    UICollectionView should be a better choice to display that kind of data in my opinion, but it's not compatible with ios5.

    If you need an ios 5 compatibility, check out that answer: https://stackoverflow.com/a/16039194/2707614

    Finally, to respond to your question, you can try to use Grand Central Dispatch as explained here

    It should look like that (I don't have a computer to test):

    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];
        });
    
    }
    });
    

    0 讨论(0)
提交回复
热议问题