why the label never replace the previous value to new value?

| | August 7, 2015

The label is in my tableview:

UILabel *label1;
    label1=[[UILabel alloc]initWithFrame:CGRectMake(500,75, 50, 50)];
    label1.text=[arr_count objectAtIndex:indexPath.row];
    [label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
    label1.tag=100;
    [cell addSubview:label1];

increment occurs here using button action

       - int tagValue=[[arrCount objectAtIndex:click.tag] intValue];
    if(tagValue <=5)
        {

        tagValue++;
            NSNumber *num=[NSNumber numberWithInt:click.tag];
            [arrCount replaceObjectAtIndex:click.tag withObject:num];
           NSLog(@"increment %@",arrCount);
        }
     NSIndexPath *ind=[NSIndexPath indexPathWithIndex:click.tag];
    label2.text = arrCount[click.tag];
    [self.mTableView.tableView reloadInputViews];
}

increment the values and i stored it to an array.the array values given to the label.i am mentioned the problem as a image.

enter image description here [i get output like this after i increment ]

1

One Response to “why the label never replace the previous value to new value?”

  1. I’ve refactored a bit your code, and it seems to work:

    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:@[@0, @1, @2, @3, @4, @5, @6, @7, @8, @9]];
    NSLog(@"original %@", array);
    for (int i = 0; i < array.count; i++) {
        int value = [array[i] intValue];
        value += 1;
    
        [array replaceObjectAtIndex:i withObject:@(value)];
        NSLog(@"incremented %@", array);
    }
    

    Output:

    original (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    incremented (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    

    And some notices:

    1. That’s highest time to switch to modern obj-c. Write
      label1.text=[arr_count[indexPath.row]; or arr_count[click.tag].
    2. Your code is insecure. Your app may crash because of out of index. Before getting tagvalue (btw you should call it tagValue according to obj-c style) you should check if the array contains so many objects
    3. Name thing correctly to increase readability of your code:
      • arr_count is a bad name, 1st in objc we don’t use _ in names, 2nd we don’t use shortcuts, 3rd it suggest its a counter – number, not the array
      • tagvalue is also bad, each word after word 0 should have capital letter, so it should be tagValue.
    4. Lastly, isn’t it better to keep NSNumber instead of NSString with number value in your array? More formatting but it’s much more correct!

    So now let’s fix the real bug: not only you have to swap value in the array but you must update the value of the label after swapping the value in the array – that’s why it isn’t working. The label.text copies the value from the array and won’t update if you update your array. You need to manually update the label according to your array.

    So after your code you should write:

    label1.text = arr_count[click.tag];
    

    However I strongly recommend to refactor your code to the mentioned points, to make your code better and improve your developer skills :)

Leave a Reply