2011-05-09 169 views
0

我正在尝试在每次迭代中更新循环中的UILabel文本,但它仅显示最后一个值,是否需要花费时间来完成循环大约是30-50秒。以编程方式更新UILabel

下面是代码:

for (float i=0; i< [topicNew count]; i++) { 

    NSDictionary *new= [topicNew objectAtIndex:i]; 
    NSString *imageName = [[[NSString alloc] initWithFormat:@"%@.%@.%@.png", appDelegate.subject,topicNamed,[new objectForKey:kWordTitleKey]] autorelease]; 
    NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:imageName]; 
    NSData *imageData = [self ParsingImagePath:[new objectForKey:kWordImagePathKey]]; 
    [progressView setProgress:i/[topicNew count]]; 
    [self setProgress:i/[topicNew count]]; 
    [self.lbltest setText:@"Image Downloading..."]; 
    self.lbltest.text =imageName; 
    //NSLog(@"sending %f",i/[topicNew count]); 
    //lblpercent.text = [NSString stringWithFormat:@"%d",i]; 
    [lblpercent setText:[[NSString stringWithFormat:@"%.0f",i/[topicNew count]] stringByAppendingString:@"%"]]; 
    //[self.view addSubview:viewalert]; 
    [self updateLabel:self]; 
    NSLog(@"%@,%d",imageName,i); 
    if(imageData != nil) 
     [imageData writeToFile:imagePath atomically:YES]; 
    else 
     [[NSFileManager defaultManager] removeItemAtPath:imagePath error:NULL]; 

    NSMutableDictionary *newWord = [NSMutableDictionary dictionaryWithObjectsAndKeys:[new objectForKey:kWordTitleKey], kWordTitleKey, [new objectForKey:kWordDefinitionKey], kWordDefinitionKey, imagePath, kWordImagePathKey, appDelegate.subject,kSubjectKey,topicName,kTopicKey,[new objectForKey:kWordMemorizedKey], kWordMemorizedKey, nil]; 
    [newTopic addObject:newWord]; 

} 
+0

回答一个问题是完全不可能的......没有问题。 – marzapower 2011-05-09 12:32:49

+0

顺便说一下,您在哪里定义了UILabel属性?您是以编程方式还是通过Interface Builder生成它? – marzapower 2011-05-09 12:33:38

+0

必须在这里同意marzapower。在我看来,你有两个问题:你只能得到标签上的最后一个值,而不是所有的......并且你的循环花费很长时间才能完成。请正确定义您的问题,以便有帮助的人不必提出有关您问题的问题。 – Joetjah 2011-05-09 12:39:02

回答

2

我认为这个问题是你的线程是由您的环锁。所以你需要在后台执行你的方法并使用

[self performSelectorInBackground:@selector(myMethod) withObject:nil]; 

不要忘记把“myMethod”;

-(void)myMethod{ 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
//I do my label update 
[pool release]; 
} 
+0

谢谢Rohin这对我来说真的很有用。谢谢。 – Arvind 2011-05-09 13:27:53

+0

没问题,我很高兴我帮你! – TheRonin 2011-05-09 13:28:54

0

您现在使用它的方式确实取代了文字。我不知道你想要更新哪个标签。如果要更新,例如,lbltest有什么东西被下载的时候,你应该改变self.lbltest.text = imageName;[self.lbltest setText:@"%@ %@", self.lbltest.text, imageName];

这样一来,你的旧文本不会被你的新的文本替换,但你的新文本被添加到你的旧文本。使用labelName.text = @"Something";将该标签上的文字更改为Something

[labelName setText:@"Something"];也是如此。

每当您使用上述两种方法中的任何一种时,都会将该标签中的文本替换为文本“Something”。如果您想为该标签添加内容,则必须在旧字符串中包含旧文本。