以编程方式更新 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];
}
I am trying to updating UILabel text in a loop in every iteration but it is displaying only the last value whether there is time taken for completing the loop is about 30 -50 sec.
Here is the code:
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题是你的线程被循环锁定。所以你需要在后台执行你的方法并使用
不要忘记放入“myMethod”;
I think the problem is your thread is lock by your loop. So you need to perform your method in background and use
Don't forget to put in "myMethod";
您现在使用它的方式确实取代了文本。我不知道您要更新哪个标签。例如,如果您想更新
lbltest
以及一直在下载的内容,则应将self.lbltest.text = imageName;
更改为[self. lbltest setText:@"%@ %@", self.lbltest.text, imageName];
这样,您的旧文本不会被新文本替换,但新文本会添加到旧文本中。
使用
labelName.text = @"Something";
将该标签上的文本更改为Something
。[labelName setText:@"Something"];
也是如此。每当您使用上述两者中的任何一个时,您都会将该标签中的文本替换为文本“Something”。如果您想向该标签添加某些内容,则必须将旧文本包含在新字符串中。
The way you are using it now, replaces the text indeed. I don't know which label you'd like to update. If you want to update, for example,
lbltest
with what's being downloaded all the time, you should changeself.lbltest.text = imageName;
to[self.lbltest setText:@"%@ %@", self.lbltest.text, imageName];
That way, your old text doesn't get replaced by your new text, but your new text gets added to your old text.
Using
labelName.text = @"Something";
changes the text on that label toSomething
.The same goes for
[labelName setText:@"Something"];
.Whenever you use any of the two described above, you'll replace the text in that label with the text "Something". If you want to add something to that label, you have to include the old text in your new String.