以编程方式更新UILabel

时间:2011-05-09 12:27:15

标签: iphone loops uilabel

我试图在每次迭代中循环更新UILabel文本,但是它只显示最后一个值,是否有完成循环的时间大约是30-50秒。

以下是代码:

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

}

2 个答案:

答案 0 :(得分:2)

我认为问题是你的线程被你的循环锁定了。所以你需要在后台执行你的方法并使用

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

不要忘记输入“myMethod”;

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

答案 1 :(得分:0)

现在使用它的方式确实取代了文本。我不知道您要更新哪个标签。如果您想要更新lbltest以及随时下载的内容,则应将self.lbltest.text = imageName;更改为[self.lbltest setText:@"%@ %@", self.lbltest.text, imageName];

这样,旧文本就不会被新文本替换,但新文本会添加到旧文本中。

使用labelName.text = @"Something";将该标签上的文字更改为Something

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

无论何时使用上述两种方法中的任何一种,您都将使用文本“Something”替换该标签中的文本。如果要向该标签添加内容,则必须在新String中包含旧文本。