2013-08-27 38 views
0

for循環看起來像這樣,我在視圖中寫入了負載,因此需要更多時間才能加載此頁面。Asynchronus for iphone中的循環

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

}

這裏笏讓我的問題是從服務器每次所以這變慢獲取圖像,請建議如何讓它做懶加載....

感謝提前

+0

rssItems中通常會有多少物品? – James

+0

http://stackoverflow.com/a/11728134/1059705和這個http://jeffreysambells.com/2013/03/01/asynchronous-operations-in-ios-with-grand-central-dispatch – Bala

+0

@James它的動態,它可以sumtime 10 sumtime 50也... – KSR

回答

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

} 
}); 
+0

你不應該在後臺隊列中使用UI對象。 *一般*包括創建它們(除非在最近的版本中已經改變)。同樣,請注意,這樣做假設'rssParser'等等,都是線程安全的... – bbum

+0

我曾經在後臺隊列中創建ui對象並在主線程上調用「顯示相關」方法,但沒有問題,感興趣的人知道爲什麼只創建對象是不好的。 – Alban

+0

從文檔:***注意:**大多數情況下,UIKit類只能在應用程序的主線程中使用。對於從UIResponder派生的類或者以任何方式涉及操縱應用程序用戶界面的類尤其如此。它只能巧合使用。 – bbum