从NSMutableArray中搜索NSString并在UiTableView中添加

时间:2013-12-12 12:19:49

标签: ios string search nsmutablearray nspredicate

我包含国家/地区列表。比如

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

从这个数组我需要搜索如阿尔巴尼亚的排序列表所有国家都以字母A开头,然后排序Al等等,然后我必须在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);

2 个答案:

答案 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]);

答案 1 :(得分: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];

完成了。希望它有所帮助:)