Sudheer Kolasani
Sudheer Kolasani

Reputation: 283

How to update Label Programmatically in iOS?

I am facing a problem with updating my labels. It doesn't remove the old names so new names go on top of old ones. any help with this will be appreciated..

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

    }
    }

enter image description here

Labels are doesn't remove the old names so new names go on top of old ones like this...

enter image description here

Note : titleLabel is "Nice Product" And reviewLabel is "This is Amazing"

Upvotes: 1

Views: 1252

Answers (5)

Russell
Russell

Reputation: 5554

ok - first create the arrays for your titles as members of your view controller

NSMutableArray *titleLabels     = [NSMutableArray array];
NSMutableArray *reviewLabels    = [NSMutableArray array];

and then update your function to look more like this

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

each time you call the function, if there are more titles returned than you have labels for, it will create new instances, and add them to your array. As you step through the array, it will re-use the instances.

Upvotes: 0

Aamir
Aamir

Reputation: 17007

I don't know why you creating new UILabel every time if you don't want to keep old UILabel text.
Let say you have IBOutlet titleLabel, then you not need to create new UILabel every time. You can just change text of existing UILabel in your view: For example

[self.titleLabel setText:titleStr];

It also reduces/saves effort of setting various properties of UILabel again and again. Only thing required is to set relevant properties of UILabel in Interface Builder and you are done.
Note: If you want to keep old UILabel as well then you need entirely different logic.

Let say you want to show different number of Reviews every time. In that case you can remove all subviews of self.viewReview by following code:

    for (UIView * subUIView in self.viewReview.subviews)
    {
        [subUIView removeFromSuperview];
    }

Upvotes: 2

user5421830
user5421830

Reputation:

  • Please change following lines after NSLog in your answer. titleLabel.text=@""; titleLabel.text = titleStr; titleLabel.textColor = [UIColor greenColor]; titleLabel.font =[UIFont fontWithName:@"Arial-BoldMT" size:16.0]; reviewLabel.font = [UIFont systemFontOfSize:12]; reviewLabel.text =@""; reviewLabel.text =reviewStr;

Upvotes: 0

DilumN
DilumN

Reputation: 2895

Remove all your subviews before adding a new one as below

    NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
    [v removeFromSuperview];
}

Above code should add before this below line,

[self.viewReview addSubview:titleLabel];
[self.viewReview addSubview:reviewLabel];

Upvotes: 3

Russell
Russell

Reputation: 5554

You are recreating the labels each time you go through the loop.

You should create them once, in an array of UILabels, and re-use them - extending the array if you need to add new ones.

Upvotes: 0

Related Questions