2013-12-12 32 views
1

我有包含国家列表。如从NSMutableArray搜索NSString并在UiTableView中添加

countries_list = [NSMutableArray arrayWithObjects:@"Afghanistan",@"Albania", 
@"Bolivia",@"Bulgaria",@"China", @"Estonia",@"France",@"Finland", 
@"Greenland",@"Iceland",@"Japan", nil]; 

从这个数组,我需要为阿尔巴尼亚排序列表中的所有国家都与该字母开头的排序铝等开始,然后,然后我必须在UITableView中添加这些值搜查。 我只需要搜索帮助我,如果有人可以。感谢

NSMutableArray* containsAnother = [NSMutableArray array]; 
NSMutableArray* doesntAnother = [NSMutableArray array]; 

for (NSString* item in inputArray) 
{ 
    if ([item rangeOfString:@"Finland"].location != NSNotFound){ 
     [containsAnother addObject:item]; 

    NSLog(@"country is found..%@",containsAnother); 

    }else{ 


    } 
     [doesntContainAnother addObject:item]; 
    NSLog(@"country is not found..%@",doesntContainAnother); 

} 



NSLog(@"array is found..%@",containsAnother); 
+0

您期待什么类型的搜索? –

回答

0

尝试使用NSPredicate predicateWithFormat:方法

countries_list = [NSMutableArray arrayWithObjects:@"Afghanistan",@"Albania",@"Bolivia",@"Bulgaria",@"China", @"Estonia",@"France",@"Finland", @"Greenland",@"Iceland",@"Japan", nil]; 

NSPredicate *predicte = [NSPredicate predicateWithFormat: 
          @"Self beginswith[c] %@", @"Albania"]; 
NSArray *filteredArray = [countries_list filteredArrayUsingPredicate:predicte];  
NSLog([filteredArray description]); 
0

你可以使用NSPredicate类此这样的: -

NSMutableArray * array = [NSMutableArray array]; 

// Loop to fetch Team Names and store in NSMutableArray named array 
for (NSDictionary *data in mySortedArray) { 
    NSString *TEAMNAME = @"TeamName"; 
    NSString *countryName = @"Australia";  

NSDictionary sortDictionary = [NSDictionary dictionaryWithObjectsAndKeys:countryName,TEAMNAME,nil]; 
[array addObject:sortDictionary]; 
} 

NSSortDescriptor * ratingDescriptor =[[[NSSortDescriptor alloc] initWithKey:TEAMNAME ascending:YES] autorelease]; 
NSArray * descriptors = [NSArray arrayWithObjects:ratingDescriptor, nil]; 
mySortedArray = (NSMutableArray *)[array sortedArrayUsingDescriptors:descriptors]; 

,它完成。希望它有助于:)