【问题标题】:Search of UITableViewUITableView的搜索
【发布时间】: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


【解决方案1】:

您可以通过内置循环方法手动构建数组,

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

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

-NSArray Documentation

-NSString Documentation

【讨论】:

  • 我不明白这与我访问数据的方式有什么关系。我在我的问题中添加了一些代码来阐明我是如何访问数据的,
  • 您是要通过字典值对数据进行排序,还是要通过数据进行搜索并获得特定结果?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多