【发布时间】:2014-04-01 19:03:45
【问题描述】:
我有一个使用 NSDictionary 和 NSArray 从 JSON 数据提要填充的 UITableView,我正在尝试使用此代码搜索 UITableView 数据,但根据我的设置方式,它无法使用其他相关信息问题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);
} 这就是数据的解析方式
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];
【问题讨论】:
-
在我的脑海中,我可以告诉你如何过滤数组而不是使用谓词而是一个循环,如果你不介意的话......但是如果你一心想要 NSPredicate 那么你将需要给我 6 小时完成回家 XD
-
如果您有更好的方法,我不会选择任何方法我愿意接受建议
-
我用我的想法发布了一个答案:3 告诉我这是否有效
-
尝试删除“SELF”。在谓词中
-
还需要重新加载你的tableView
标签: ios objective-c json uitableview uisearchbar