цикл for выглядит так, что я написал на виду, загрузился, поэтому загрузка этой страницы занимает больше времени.Asynchronus for loop in 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];
}
}
здесь Ват сделать меня проблемой является изображение, которое получает от сервера каждый раз, так что это становится медленнее, пожалуйста подсказывают, как сделать это делать, как отложенной загрузки ....
спасибо заблаговременно
Сколько предметов обычно будет в rssItems, примерно? – James
http://stackoverflow.com/a/11728134/1059705 и этот http://jeffreysambells.com/2013/03/01/asynchronous-operations-in-ios-with-grand-central-dispatch – Bala
@James его динамический , это может быть sumtime 10 sumtime 50 также ... – KSR