Search of UITableView

℡╲_俬逩灬. 提交于 2019-12-13 21:07:28

问题


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];

回答1:


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];

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.

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 Documentation

-NSString Documentation



来源:https://stackoverflow.com/questions/22794677/search-of-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!