搜索 UITableView [英] Search of UITableView

查看:16
本文介绍了搜索 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 NSDictionary 和 NSArray 从 JSON 数据馈送填充的 UITableView,我正在尝试使用此代码搜索 UITableView 数据,但它无法根据我的设置方式使用其他一些相关信息问题 直接从 JSON 解析到 UITableView 的数据

I have a UITableView populated from a JSON data feed using an NSDictionary and NSArray and I am trying to search the UITableView data using this code, but it is not working some other related info based on how I set it up is in this question Data from JSON Parsing straight to UITableView

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

// Update the filtered array based on the search text and scope.

// Remove all objects from the filtered search array
[self.filteredCandyArray removeAllObjects];

// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
NSArray *tempArray = [airportsArray filteredArrayUsingPredicate:predicate];
NSLog(@" text %@", searchText);


filteredCandyArray = [NSMutableArray arrayWithArray:tempArray];
NSLog(@"NSLog %@", scope);

}这就是数据的解析方式

} This is how the data is parsed

 NSString* path = @"https://api.flightstats.com/flex/airports/rest/v1/json/active?appId=532ca6af&appKey=50d6d3351e5953152b2688f10d10d276";


NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];

[_request setHTTPMethod:@"GET"];


NSURLResponse *response = nil;

NSError *error = nil;


NSData* _connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];

if(nil != error)
{
    NSLog(@"Error: %@", error);
}
else
{

    NSMutableDictionary* json = nil;


    if(nil != _connectionData)
    {
        json = [NSJSONSerialization JSONObjectWithData:_connectionData options:NSJSONReadingMutableContainers error:&error];
    }

    if (error || !json)
    {
        NSLog(@"Could not parse loaded json with error:%@", error);
    }
    else
    {



        routeRes = [json objectForKey:@"airports"];


         airportsArray = [json objectForKey:@"airports"];


                                   }

                                   _connectionData = nil;
                                   NSLog(@"connection done");
                                   }


NSLog(@" end array %@", candyArray);
// Initialize the filteredCandyArray with a capacity equal to the candyArray's capacity
filteredCandyArray = [NSMutableArray arrayWithCapacity:[airportsArray count]];

// Reload the table
[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

[[self tableView] reloadData];

推荐答案

您可以通过 build in loop 方法手动构建数组,

You can manually build the array via the build in loop methods,

NSMutableArray *filteredResults = [NSMutableArray new];
[airportsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
   if ([obj.name rangeOfString: searchText].location != NSNotFound) {
      [filteredResults addObject:obj];
   }
}];
self.filteredCandyArray = [NSArray arrayWithArray:filteredResults];

您需要将 id obj 替换为您正在使用的任何对象模型.例如,如果你有一个平面模型,你可以用 Plane *plane 代替 id obj.

You will need to replace the id obj with what ever object model you are using. For example if you had a plane model you might do Plane *plane in replace of id obj.

稍后我可能会通过谓词添加如何使用它,但这应该可以满足您的要求.您可以在 NSArray 文档中查看 enumerateObjectsUsingBlock: 和 arrayWithArray: 方法.rangeOfString:可以在 NSString 文档中查看.

I might add how to use it via predicates later, but this should do what you request. You may view the enumerateObjectsUsingBlock: and arrayWithArray: methods in the NSArray documentation. rangeOfString: can be viewed in the NSString documentation.

-NSArray 文档

-NSString 文档

这篇关于搜索 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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