如何以相同的方式改变NSMutable数组的顺序,另一个可变数组被更改 [英] how to change order of NSMutable array in same way another mutable arrays get changed

查看:78
本文介绍了如何以相同的方式改变NSMutable数组的顺序,另一个可变数组被更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个数组。它们是名称生日剩余天,如下所示:

I have three arrays. They are name, birthdates and remaining days like below:

    name                         birthdate                 remaining 

    "Abhi Shah",                 "01/14",                  300
    "Akash Parikh",              "12/09/1989",             264
    "Anand Kapadiya",            "12/01",                  256
    "Annabella Faith Perez",     "03/02",                  347
    "Aysu Can",                  "04/14/1992",             25
    "Chirag Pandya"              "10/07/1987"              201

我想重新安排剩余数组升序,
但同时 name birthdate 也应该以相同的方式重新排序。

I want to rearrange the remaining days array into ascending order, but at the same time name and birthdate should also get reordered in the same way.

请参阅下面的示例:

    name                         birthdate                 remaining 

    "Aysu Can",                  "04/14/1992",             25
    "Chirag Pandya"              "10/07/1987"              201
    "etc..."


推荐答案

    self.arrayForRows = [[NSMutableArray alloc]init];
    NSMutableArray *arrayForNames = [[NSMutableArray alloc]initWithObjects:@"Abhi Shah",@"Akash",@"Nagavendra",@"Ramana",@"Simhachalam", nil];
    NSMutableArray *arrayForBirthDates = [[NSMutableArray alloc]initWithObjects:@"01/14/94",@"01/14",@"11/07/87",@"12/07/89",@"23/08/91", nil];
    NSMutableArray *arrayForRemaining = [[NSMutableArray alloc]initWithObjects:@"200",@"320",@"32",@"450",@"14", nil];


    for (int i=0; i<arrayForBirthDates.count; i++)
    {
       NSMutableDictionary *tempDicts = [[NSMutableDictionary alloc]init];
       [tempDicts setObject:[arrayForNames objectAtIndex:i] forKey:@"names"];
       [tempDicts setObject:[arrayForBirthDates objectAtIndex:i] forKey:@"birth"];
       [tempDicts setObject:[NSNumber numberWithInt:[[arrayForRemaining objectAtIndex:i] intValue]] forKey:@"remaining"];
       [self.arrayForRows addObject:tempDicts];
    }  


    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"remaining" ascending:YES];
    [self.arrayForRows sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

在tableView列表中使用

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arrayForRows count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
    }
    cell.textLabel.text = [[self.arrayForRows objectAtIndex:indexPath.row] valueForKey:@"names"];
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

这篇关于如何以相同的方式改变NSMutable数组的顺序,另一个可变数组被更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆