作者aureolus (Aureo)
看板MacDev
标题[问题] 排序修改NSMutableArray单一key值
时间Tue Apr 26 11:59:17 2016
假设今天宣告
NSMutableArray * arrayA2;
NSDictionary *data;
data = @{@"No.": [NSString stringWithFormat:@"%d", 3],
@"Name": [NSString stringWithFormat:@"妙蛙种子"],};
data = @{@"No.": [NSString stringWithFormat:@"%d", 1],
@"Name": [NSString stringWithFormat:@"妙蛙花"],};
[arrayA2 addObject:data];
NSLog 去看 arrayA2的内容是
{
No = "3"
name = "妙蛙种子"
}
{
No = "1"
name = "妙蛙花"
}
请问该怎麽做才能只改key"No"的值,不改"Name",让"No"的值可以依序或倒序排列
例如
{
No = "1"
name = "妙蛙种子"
}
{
No = "3"
name = "妙蛙花"
}
谢谢!
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 60.248.134.2
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/MacDev/M.1461643159.A.817.html
※ 编辑: aureolus (61.228.67.125), 04/26/2016 12:39:15
1F:→ darktt: 要嘛就按顺序放入array,不然就是放好後重新做排序的动作 04/26 12:56
2F:→ darktt: 呃,抱歉搞错了,你只能安排array的资料完成後,才重新修 04/26 12:59
3F:→ darktt: 改No的值 04/26 12:59
你好,非常谢谢建议!
会这样做是因为我Array显示在TableView当中,且我有写能够Drag Drop的功能
但是拖拉项目後编号不会跟着改变,所以想请问有没有方法能够修正,谢谢!
※ 编辑: aureolus (60.248.134.2), 04/26/2016 13:06:49
4F:推 Blueshiva: 不过看来是针对Array里面Data的number重新排序,所以才 04/26 13:07
5F:→ Blueshiva: 会指定成1, 3,还实在是很怪的需求 04/26 13:07
谢谢回应,因为只要是显示在NSTableView中,要有拖拉功能
但是拖拉後项目编号不会跟着更新
※ 编辑: aureolus (60.248.134.2), 04/26/2016 13:11:57
6F:→ Esvent: 你可以用indexPath的值来显示编号 不用存在你的资料里面 04/26 21:56
谢谢各位帮忙,虽然这个问题很困难不过自己找到另一种方法了
for(int i =0 ; i <arrayA2.count ; i++)
{
NSDictionary *item =[arrayA2 objectAtIndex:i];
NSMutableDictionary *mutableItem = [NSMutableDictionary dictionaryWithDictionary:item];
[mutableItem setObject: [NSNumber numberWithInteger: i+1] forKey:@"No"];
[arrayA2 setObject: mutableItem atIndexedSubscript:i];
}
这样做之後成功重新依序写入所有NSMutableArray的值,但不是用排序的方式
不过已达成我原先的目的,谢谢大家帮忙
※ 编辑: aureolus (60.248.134.2), 04/27/2016 10:04:38