objective c - why the label never replace the previous value to new value? -


the label in 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 values , stored array.the array values given label.i mentioned problem image.

enter image description here [i output after increment ]

1

i've refactored bit code, , seems work:

nsmutablearray *array = [[nsmutablearray alloc] initwitharray:@[@0, @1, @2, @3, @4, @5, @6, @7, @8, @9]]; nslog(@"original %@", array); (int = 0; < 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 notices:

  1. that's highest time switch modern obj-c. write label1.text=[arr_count[indexpath.row]; or arr_count[click.tag].
  2. your code insecure. app may crash because of out of index. before getting tagvalue (btw should call tagvalue according obj-c style) should check if array contains many objects
  3. name thing correctly increase readability of code:
    • arr_count bad name, 1st in objc don't use _ in names, 2nd don't use shortcuts, 3rd suggest counter - number, not array
    • tagvalue bad, each word after word 0 should have capital letter, should tagvalue.
  4. lastly, isn't better keep nsnumber instead of nsstring number value in array? more formatting it's more correct!

so let's fix real bug: not have swap value in array must update value of label after swapping value in array - that's why isn't working. label.text copies value array , won't update if update array. need manually update label according array.

so after code should write:

label1.text = arr_count[click.tag]; 

however recommend refactor code mentioned points, make code better , improve developer skills :)


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -