2013-04-08 69 views
-1

過濾器數組中刪除鍵值城市 的副本,我需要沒有重複顯示在第一tableview中唯一的城市名稱,當沒有選擇我需要表現出特別的部門在接下來的tableview印度過濾NSArray的對象

例子我需要在一個視圖 顯示三個部門,請給我建議

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"; 
      }, 

    ) 
+0

請檢查此[鏈接] [1] [1]:http://stackoverflow.com/questions/1025674/the-best-way-to-remove-duplicate-values-from- nsmutablearray-in-objective-c 它會給你不同的價值。 – 2013-04-08 07:17:06

+0

你只想把x,y,z傳給下一個VC? – 2013-04-08 07:17:08

回答

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

負荷第一的tableView與cityArray

didSelectRowAtIndex

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"city = %@",[cityArray objectAtIndex:indexPath.row]]; 
NSArray *array = [responseArray filteredArrayUsingPredicate:predicate]; 
NSLog(@"Array -- %@",array); 
+0

坦克你非常 – VishnuVardhan 2013-04-08 07:37:44

0

使用下面的代碼提供您的輸出rawJSONOutput

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]; 
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
    NSArray * sortedArray = [[NSMutableArray alloc] initWithArray:[rawJSONOutput sortedArrayUsingDescriptors:sortDescriptors]]; 
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; 
} 
0

同時使陣列本身的字典,檢查城市是否已經存在,如果是,添加使用像department1,部門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"; 
    }, 

    ) 

更好的嘗試做成這樣。這將是容易的。