-1

过滤数组删除键值城市的重复项我需要在第一个表视图中仅显示城市名称而没有重复项以及何时选择我需要在下一个表视图中显示特定部门

以印度为例,我需要在下一个视图中显示三个部门请建议我

Array values :(
        {
        city = uk;
        department = "Sales support";

    },
        {

        city = us;
        department = "Sales support";

    },
        {
          city = italy;
          department = "Sales support";
           },
        {

        city = india;
        department = "x";
    },
        {

        city = india;
        department = "y";
            },
        {

        city = india;
        department = "z";
           },

   )
4

4 回答 4

2
for (int i = 0; i< responseArray.count; i++)
{
    if (![cityArray containsObject:[[responseArray objectAtIndex:i] valueForKey:@"city"]])
    {
          [cityArray addObject:[[responseArray objectAtIndex:i] valueForKey:@"city"]];
    }
}

使用 cityArray 加载第一个 tableView

didSelectRowAtIndex

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"city = %@",[cityArray objectAtIndex:indexPath.row]];
NSArray *array = [responseArray filteredArrayUsingPredicate:predicate];
NSLog(@"Array -- %@",array);
于 2013-04-08T07:22:04.397 回答
0

使用以下代码将您的输出提供给 rawJSONOutput

   NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray * sortedArray = [[NSMutableArray alloc] initWithArray:[rawJSONOutput sortedArrayUsingDescriptors:sortDescriptors]];
于 2013-04-08T07:10:46.367 回答
0

您可以使用NSPredicate查询过滤数组/字典。

例子:

NSArray *someArray()
{
    NSString *const city = @"city";
    NSString *const department = @"department";
    NSString *const uk = @"uk";
    NSString *const us = @"us";
    NSString *const italy = @"italy";
    NSString *const india = @"india";

    return @[
     @{ city: uk, department: @"Sales support" },
     @{ city: us, department: @"Sales support" },
     @{ city: italy, department: @"Sales support" },
     @{ city: india, department: @"x" },
     @{ city: india, department: @"y" },
     @{ city: india, department: @"z" },
    ];
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        NSArray *array = someArray();

        NSLog(@"%@", array);

        // You can make many fancy queries.
        NSPredicate *p = [NSPredicate predicateWithFormat:@"SELF.city like 'india'"];
        NSArray *b = [array filteredArrayUsingPredicate:p];

        NSLog(@"%@", b);

    }
    return 0;
}
于 2013-04-08T07:23:14.107 回答
0

在制作数组本身的字典时,检查城市是否已经存在,如果存在,则使用另一个键添加部门,例如部门1,部门2..

Array values :(
        {
        city = uk;
        department = "Sales support";

    },
        {

        city = us;
        department = "Sales support";

    },
        {
          city = italy;
          department = "Sales support";
           },
        {

        city = india;
        department1 = "x";
        department2 = "y";
       department3 = "z";
    },

   )

最好试着做这样的。这将很容易。

于 2013-04-08T07:26:17.103 回答