How to change the values dynamically in the NSMutableArray in iPhone? -



How to change the values dynamically in the NSMutableArray in iPhone? -

i have 10 buttons , wrote button action using switch case. when click each button, increment amount of values. if using 10 different variables set count value variable, work. other possible way accomplish this?

i want alter values dynamically in mutable array.

for eg:

-(ibaction) btnaction : (id) sender{ switch (btn.tag) { case 0: // amountarray mutable array. startcococount = startcococount + 10; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 1: startcococount = startcococount + 15; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 2: startcococount = startcococount + 20; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 3: startcococount = startcococount + 25; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 4: startcococount = startcococount + 30; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 5: startcococount = startcococount + 35; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 6: startcococount = startcococount + 40; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 7: startcococount = startcococount + 50; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 8: startcococount = startcococount + 60; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 9: startcococount = startcococount + 20; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; case 10: startcococount = startcococount + 20; //nsstring *str = [nsstring stringwithformat:@"%d", startcococount]; //[amountarray addobject:str]; break; default: break; } }

in table view,

cell.textlabel.text = [amountarray objectatindex:indexpath];

if using single variable, won't set conditions, how can accomplish this?

you can seek this. work.

int startcococount; nsmutablearray *dataarray = [[nsmutablearray alloc] init]; for(int i=0;i<11;i++) { [dataarray addobject:@"0"]; }

and method follows:

-(ibaction) btnaction : (id) sender{ switch ([sender tag]) { case 0: startcococount = startcococount + 10; [dataarray replaceobjectatindex:0 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:0] intvalue] + startcococount]]; break; case 1: startcococount = startcococount + 15; [dataarray replaceobjectatindex:1 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:1] intvalue] + startcococount]]; break; case 2: startcococount = startcococount + 20; [dataarray replaceobjectatindex:2 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:2] intvalue] + startcococount]]; break; case 3: startcococount = startcococount + 25; [dataarray replaceobjectatindex:3 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:3] intvalue] + startcococount]]; break; case 4: startcococount = startcococount + 30; [dataarray replaceobjectatindex:4 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:4] intvalue] + startcococount]]; break; case 5: startcococount = startcococount + 35; [dataarray replaceobjectatindex:5 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:5] intvalue] + startcococount]]; break; case 6: startcococount = startcococount + 40; [dataarray replaceobjectatindex:6 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:6] intvalue] + startcococount]]; break; case 7: startcococount = startcococount + 50; [dataarray replaceobjectatindex:7 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:7] intvalue] + startcococount]]; break; case 8: startcococount = startcococount + 60; [dataarray replaceobjectatindex:8 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:8] intvalue] + startcococount]]; break; case 9: startcococount = startcococount + 20; [dataarray replaceobjectatindex:9 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:9] intvalue] + startcococount]]; break; case 10: startcococount = startcococount + 20; [dataarray replaceobjectatindex:10 withobject:[nsstring stringwithformat:@"%d", [[dataarray objectatindex:10] intvalue] + startcococount]]; break; default: break; } }

let me know if have question.

iphone nsmutablearray

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -