如何在 iOS 中以编程方式更新标签?
How to update Label Programmatically in iOS?
我在更新标签时遇到问题。它不会删除旧名称,因此新名称位于旧名称之上。任何帮助将不胜感激..
NSDictionary *arrDictionary = [NSJSONSerialization JSONObjectWithData:requestHandler options:0 error:nil];
NSDictionary *vendorDic = [arrDictionary objectForKey:@"feed"];
NSLog(@" the response is %@",vendorDic);
NSArray * titleArray = [vendorDic valueForKey:@"title"];
NSArray * reviewArray = [vendorDic valueForKey:@"review"];
if (titleArray.count == 0) {
self.scrollViewReview.contentSize = CGSizeMake(0*172, _scrollViewReview.frame.size.height);
}
else{
NSUInteger x =[titleArray count];
self.scrollViewReview.contentSize = CGSizeMake(x*172, _scrollViewReview.frame.size.height);
for(int i = 0;i <= titleArray.count-1;i = i + 1){
_noReview.hidden = YES;
UILabel * titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(i*172, 0, 100, 50)];
UILabel * reviewLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * 172 , 30, 200, 50)];
titleLabel.clearsContextBeforeDrawing =YES;
titleLabel.clipsToBounds = YES;
reviewLabel.clipsToBounds = YES;
reviewLabel.clearsContextBeforeDrawing=YES;
NSString* titleStr = [NSString stringWithFormat:@"%@", titleArray[i]];
NSString * reviewStr =[NSString stringWithFormat:@"%@",reviewArray[i]];
// NSLog(@" loop in for title: %@",titleStr);
//NSLog(@" loop in for title: %@",reviewStr);
titleLabel.text = titleStr;
titleLabel.textColor = [UIColor greenColor];
titleLabel.font =[UIFont fontWithName:@"Arial-BoldMT" size:16.0];
reviewLabel.font = [UIFont systemFontOfSize:12];
reviewLabel.text = reviewStr;//etc...
[self.viewReview addSubview:titleLabel];
[self.viewReview addSubview:reviewLabel];
}
}
Labels are doesn't remove the old names so new names go on top of old
ones like this...
Note : titleLabel is "Nice Product" And reviewLabel is "This is
Amazing"
每次循环时都在重新创建标签。
您应该在 UILabel 数组中创建它们一次,然后 re-use 它们 - 如果您需要添加新的,请扩展数组。
在添加新的子视图之前删除所有子视图,如下所示
NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
上面的代码应该在下面这一行之前添加,
[self.viewReview addSubview:titleLabel];
[self.viewReview addSubview:reviewLabel];
我不知道为什么你每次都创建新的 UILabel
如果你不想保留旧的 UILabel
文本。
假设你有 IBOutlet titleLabel
,那么你不需要每次都创建新的 UILabel
。您可以只更改视图中现有 UILabel
的文本:
例如
[self.titleLabel setText:titleStr];
它还 reduces/saves 一次又一次地设置 UILabel
的各种属性。只需要在 Interface Builder
中设置 UILabel
的相关属性即可。
注意:如果你想保留旧的 UILabel
那么你需要完全不同的逻辑。
假设您想每次显示不同数量的评论。在这种情况下,您可以通过以下代码删除 self.viewReview
的所有 subviews
:
for (UIView * subUIView in self.viewReview.subviews)
{
[subUIView removeFromSuperview];
}
- 请在您的回答中更改 NSLog 后的以下行。
titleLabel.text=@"";
titleLabel.text = titleStr;
titleLabel.text颜色
= [UIColor 绿色];
titleLabel.font =[UIFont fontWithName:@"Arial-BoldMT" size:16.0];
reviewLabel.font = [UIFont
systemFontOfSize:12];
reviewLabel.text =@"";
reviewLabel.text =reviewStr;
好的 - 首先为您的标题创建数组作为视图控制器的成员
NSMutableArray *titleLabels = [NSMutableArray array];
NSMutableArray *reviewLabels = [NSMutableArray array];
然后更新您的函数,使其看起来更像这样
for(int i = 0;i <= titleArray.count-1;i = i + 1){
_noReview.hidden = YES;
UILabel * titleLabel;
UILabel * reviewLabel;
if (i >= [titleLabels count]) {
titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(i*172, 0, 100, 50)];
reviewLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * 172 , 30, 200, 50)];
[titleLabels addObject:titleLabel];
[reviewLabels addObject:reviewLabel];
}
else {
titleLabel = [titleLabels objectAtIndex:i];
reviewLabels = [reviewLabels objectAtIndex:i];
}
// do your other stuff here
每次调用函数时,如果返回的标题多于标签,它将创建新实例,并将它们添加到数组中。当您遍历数组时,它将 re-use 个实例。
我在更新标签时遇到问题。它不会删除旧名称,因此新名称位于旧名称之上。任何帮助将不胜感激..
NSDictionary *arrDictionary = [NSJSONSerialization JSONObjectWithData:requestHandler options:0 error:nil];
NSDictionary *vendorDic = [arrDictionary objectForKey:@"feed"];
NSLog(@" the response is %@",vendorDic);
NSArray * titleArray = [vendorDic valueForKey:@"title"];
NSArray * reviewArray = [vendorDic valueForKey:@"review"];
if (titleArray.count == 0) {
self.scrollViewReview.contentSize = CGSizeMake(0*172, _scrollViewReview.frame.size.height);
}
else{
NSUInteger x =[titleArray count];
self.scrollViewReview.contentSize = CGSizeMake(x*172, _scrollViewReview.frame.size.height);
for(int i = 0;i <= titleArray.count-1;i = i + 1){
_noReview.hidden = YES;
UILabel * titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(i*172, 0, 100, 50)];
UILabel * reviewLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * 172 , 30, 200, 50)];
titleLabel.clearsContextBeforeDrawing =YES;
titleLabel.clipsToBounds = YES;
reviewLabel.clipsToBounds = YES;
reviewLabel.clearsContextBeforeDrawing=YES;
NSString* titleStr = [NSString stringWithFormat:@"%@", titleArray[i]];
NSString * reviewStr =[NSString stringWithFormat:@"%@",reviewArray[i]];
// NSLog(@" loop in for title: %@",titleStr);
//NSLog(@" loop in for title: %@",reviewStr);
titleLabel.text = titleStr;
titleLabel.textColor = [UIColor greenColor];
titleLabel.font =[UIFont fontWithName:@"Arial-BoldMT" size:16.0];
reviewLabel.font = [UIFont systemFontOfSize:12];
reviewLabel.text = reviewStr;//etc...
[self.viewReview addSubview:titleLabel];
[self.viewReview addSubview:reviewLabel];
}
}
Labels are doesn't remove the old names so new names go on top of old ones like this...
Note : titleLabel is "Nice Product" And reviewLabel is "This is Amazing"
每次循环时都在重新创建标签。
您应该在 UILabel 数组中创建它们一次,然后 re-use 它们 - 如果您需要添加新的,请扩展数组。
在添加新的子视图之前删除所有子视图,如下所示
NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
上面的代码应该在下面这一行之前添加,
[self.viewReview addSubview:titleLabel];
[self.viewReview addSubview:reviewLabel];
我不知道为什么你每次都创建新的 UILabel
如果你不想保留旧的 UILabel
文本。
假设你有 IBOutlet titleLabel
,那么你不需要每次都创建新的 UILabel
。您可以只更改视图中现有 UILabel
的文本:
例如
[self.titleLabel setText:titleStr];
它还 reduces/saves 一次又一次地设置 UILabel
的各种属性。只需要在 Interface Builder
中设置 UILabel
的相关属性即可。
注意:如果你想保留旧的 UILabel
那么你需要完全不同的逻辑。
假设您想每次显示不同数量的评论。在这种情况下,您可以通过以下代码删除 self.viewReview
的所有 subviews
:
for (UIView * subUIView in self.viewReview.subviews)
{
[subUIView removeFromSuperview];
}
- 请在您的回答中更改 NSLog 后的以下行。 titleLabel.text=@""; titleLabel.text = titleStr; titleLabel.text颜色 = [UIColor 绿色]; titleLabel.font =[UIFont fontWithName:@"Arial-BoldMT" size:16.0]; reviewLabel.font = [UIFont systemFontOfSize:12]; reviewLabel.text =@""; reviewLabel.text =reviewStr;
好的 - 首先为您的标题创建数组作为视图控制器的成员
NSMutableArray *titleLabels = [NSMutableArray array];
NSMutableArray *reviewLabels = [NSMutableArray array];
然后更新您的函数,使其看起来更像这样
for(int i = 0;i <= titleArray.count-1;i = i + 1){
_noReview.hidden = YES;
UILabel * titleLabel;
UILabel * reviewLabel;
if (i >= [titleLabels count]) {
titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(i*172, 0, 100, 50)];
reviewLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * 172 , 30, 200, 50)];
[titleLabels addObject:titleLabel];
[reviewLabels addObject:reviewLabel];
}
else {
titleLabel = [titleLabels objectAtIndex:i];
reviewLabels = [reviewLabels objectAtIndex:i];
}
// do your other stuff here
每次调用函数时,如果返回的标题多于标签,它将创建新实例,并将它们添加到数组中。当您遍历数组时,它将 re-use 个实例。