How to change the values dynamically in the NSMutableArray in iPhone?
I have 10 buttons and i wrote the button action using switch case. When i click each button, it will increase some amount of values. If i am using 10 different variables for set the count value variable, it will work. But is any other possible way to achieve this?
I want to change the values dynamically in the mutable array.
For Eg:
-(IBAction) btnAction : (id) sender{
switch (btn.tag) {
case 0:
// amountArray is mutable array.
startCocoCount = startCocoCount + 10;
//NSString *str = [NSString stringWithFormat:@"%d", startCocoCount];
开发者_如何学JAVA //[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 i am using single variable, it won't set for all the conditions, so how can i achieve this?
You can try this. It will work.
int startCocoCount;
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for(int i=0;i<11;i++)
{
[dataArray addObject:@"0"];
}
And for the method it will as 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 you have any question.
NSNumber
allows you to store into an array. To access the array by index you will need to pre-populate the array with NSNumber
s so that the indexes are available.
//You will need to know the index otherwise use a dictionary with named keys
NSNumber *num = [amountArray objectAtIndex:index];
num = [NSNumber numberWithInt:[num intValue] + 20];
[amountArray replaceObjectAtIndex:index withObject:num];
I think you can simplify like this
-(IBAction) btnAction : (id) sender{
startCocoCount = startCocoCount + btn.tag;
}
You can set the tags of the button to hold the values.
精彩评论