[英]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];
}
}
標簽不會刪除舊名稱,因此新名稱會像這樣在舊名稱之上...
注意:titleLabel是“不錯的產品”,reviewLabel是“這很棒”
在添加新子視圖之前,請先刪除所有子視圖,如下所示
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];
它還減少/節省了一次又一次設置UILabel
各種屬性的工作。 只需要做的就是在Interface Builder
設置UILabel
相關屬性即可。
注意:如果還要保留舊的UILabel
,則需要完全不同的邏輯。
假設您想每次顯示不同數量的評論。 在這種情況下,您可以通過以下代碼刪除self.viewReview
的所有subviews
:
for (UIView * subUIView in self.viewReview.subviews)
{
[subUIView removeFromSuperview];
}
每次循環時都在重新創建標簽。
您應該在一個UILabel數組中一次創建它們,然后重新使用它們-如果需要添加新數組,則可以擴展該數組。
好的-首先為您的標題創建數組作為視圖控制器的成員
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
每次調用該函數時,如果返回的標題多於標簽的標題,它將創建新實例,並將其添加到數組中。 在遍歷數組時,它將重用實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.