[英]How to store the value into NSMutableArray using button tags in iPhone?
I have 10 buttons, when clicks the buttons will increase the some amount of values.我有 10 个按钮,当单击按钮时会增加一些值。 I want to put the values into NSMutableArray.
我想将值放入 NSMutableArray。 But i don't want to add the values which depends on the button index, i want to add the values into the array as the same order (0 to ArrayCount) when clicks any button in randomly.
但是我不想添加取决于按钮索引的值,我想在随机单击任何按钮时将值以相同的顺序(0 到 ArrayCount)添加到数组中。
For Eg:例如:
switch (btn.tag) {
case 0:
itemValue = itemValue + 15; //item value is 30
[amountArray addObject:itemValue];
break;
case 1:
itemValue = itemValue + 30; //item value is 60
[amountArray addObject:itemValue];
break;
case 2:
itemValue = itemValue + 25; //item value is 50
[amountArray addObject:itemValue];
break;
case 3:
itemValue = itemValue + 40; //item value is 80
[amountArray addObject:itemValue];
break;
case 4: //item value is 45
itemValue = itemValue + 15;
[amountArray addObject:itemValue];
break;
In this case, i have clicked the button in the order of the button index 4,3,2,1,0 and expected result array is在这种情况下,我按按钮索引 4、3、2、1、0 的顺序单击了按钮,预期结果数组是
{45,80,50,60,30}.
If i have used something like this,如果我用过这样的东西,
[amountArray replaceObjectAtIndex:btn.tag withObject:itemValue];
and the result is {30,60,50,80,45}.结果是 {30,60,50,80,45}。
But i want to the store the values as normally in the array as 0 to arrayCount.但我想将数组中的值正常存储为 0 到 arrayCount。 When clicks the button every time, it will increase some value, so shouldn't allow the duplicate values , have to change only values.
每次单击按钮时,都会增加一些值,因此不应允许重复值,必须只更改值。
Thanks!谢谢!
NSMutableSet will avoid dublicate. NSMutableSet 将避免重复。
so instead of using array just use NSMutableSet.所以不要使用数组,而是使用 NSMutableSet。
NSMutableSet *amountSet=[[NSMutableSet alloc]init];
[amountSet addObject:itemValue];
You are adding an int to array that is wrong you need to use您正在将一个 int 添加到您需要使用的错误的数组中
[amountArray addObject:[NSNumber numberWithInt:itemValue]];
Also you need to check if that value is available in array already by using the method of array您还需要使用数组方法检查该值是否已在数组中可用
if([array indexOfObject:[NSNumber numberWithInt:itemValue]] == NSNotFound) {
//Add object here to mutable array
}
or not and later you need to sort array using sortDescripter or NSPredicate.与否,稍后您需要使用 sortDescripter 或 NSPredicate 对数组进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.