【问题标题】:How should I use searchController我应该如何使用 searchController
【发布时间】:2016-04-29 07:48:28
【问题描述】:

tableView 方法中应该如何使用搜索控制器而不是 searchDisplayController

我这里有一个示例代码

if (tableView == self.searchDisplayController.searchResultsTableView)

我在这行收到错误,它说 searchDisplayController 已弃用。

 @implementation LocationsViewController 

 {

NSArray *_park;

 }

  - (void)viewDidLoad {
[super viewDidLoad];

self.searchResult = [NSMutableArray arrayWithCapacity:[self->_park     count]];


   // Create a new JSONLoader with a local file URL
   JSONLoader *jsonLoader = [[JSONLoader alloc] init];
   NSURL *url = [[NSBundle mainBundle] URLForResource:@"park" withExtension:@"json"];

// Load the data on a background queue...
// As we are using a local file it's not really necessary, but if we were connecting to an online URL then we'd need it
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    _park = [jsonLoader locationsFromJSONFile:url];
    // Now that we have the data, reload the table data on the main UI thread
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
});

}

  //Just before showing the LocationDetailViewController, set the selected Location object
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
LocationDetailViewController *vc = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
vc.location = [_park objectAtIndex:indexPath.row];

}

 #pragma mark - Table View Controller Methods
 //UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];




  - (UITableViewCell *)tableView:(UITableView *)tableView   cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LocationCell"];

Location *location = [_park objectAtIndex:indexPath.row];

cell.textLabel.text = location.name;
cell.detailTextLabel.text = location.address;

// cell.detailTextLabel.text = location.ide; cell.imageView.image = [UIImage imageNamed:@"location_image"];

 ///////////////////

   if (tableView == self.searchDisplayController.searchResultsTableView)
{
    cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];
}
else
{
    cell.textLabel.text = self->_park[indexPath.row];
}

 /////////////////

return cell;

}

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 //////////////////////////////////////////////


if (tableView == self.searchDisplayController.searchResultsTableView)
{
    return [self.searchResult count];
}
else
{
    return [self->_park count];
}


 //////////////////////////////////////////////

return [_park count];

}

 /////////////////////////////////////////////////



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

{ [self.searchResult removeAllObjects]; NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];

self.searchResult = [NSMutableArray arrayWithArray: [self->_park filteredArrayUsingPredicate:resultPredicate]];

}

    -(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{

    [self filterContentForSearchText:searchString scope: [[self.searchDisplayController.searchBar scopeButtonTitles]  objectAtIndex:[self.searchDisplayController.searchBar  selectedScopeButtonIndex]]];


return YES;

}

@结束

【问题讨论】:

  • 您使用的是什么语言?
  • 你有这个答案然后帮助
  • 如果我不知道它是什么框架语言,我不知道如何回答。
  • 如果您尝试为您的 tableView 和 UISearchResultsController 以不同的方式处理 cellForRowAtIndexPath 或 didSelectRowAtIndexPath ,因此您上面提到的条件则 UISearchController 有一个名为 searchResultsController 的属性 :) 所以您可以比较 if (tableView == self. yourSearchController.searchResultsController) 提供了更多的代码来理解上下文,仅仅写一行代码不会有多大帮助
  • 这是我的代码@sandeep

标签: ios objective-c search controller


【解决方案1】:

我相信您已经通过将nil 作为参数传递给searchResultsController 来声明UISearchController。所以你同样的tableView 被用作searchResultsControllerUISearchController

喜欢

let searchController = UISearchController(searchResultsController: nil)

UISearchController searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

我希望您现在要做的就是区分UISeacrhController 是活动的还是您的tableView,以便您可以为它们显示不同的单元格:)

如果是这样,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LocationCell"];

    Location *location = [_park objectAtIndex:indexPath.row];

    cell.textLabel.text = location.name;
    cell.detailTextLabel.text = location.address;
 // cell.detailTextLabel.text = location.ide;
//  cell.imageView.image = [UIImage imageNamed:@"location_image"];

       if (searchController.active)
    {
        cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = self->_park[indexPath.row];
    }

     /////////////////

    return cell;
}

如果您已实例化不同的 tableView 控制器并为UISearchController 提供SearchResultsController,我在评论if (tableView == self.yourSearchController. searchResultsController) 中的回答会起作用

但是因为您在init 中指定了nil,所以UISearchControllersame tableView 实例将用作SearchResultsController。所以比较的结果总是是的:)

相反,您可以依赖UISearchControlleractive 属性并检查正在使用的上下文 tableView 并适当地处理它:)

希望我的观点清楚 :) 编码愉快 :)

【讨论】:

  • @vicky:您选择了错误的教程 :) 那是唯一的错误 :) 伙计,他正在使用 iOS8 中已弃用的 SearchDisplayController。教程的标题清楚地表明它适用于 iOS 7。无论如何我的回答对你有帮助吗???如果您想快速了解如何使用 UISearchController,请参阅 raywenderlich.com/113772/uisearchcontroller-tutorial
  • 感谢 sandeep 的帮助......但我没有通过传递 nil 来声明任何搜索控制器......所以这个答案对我没有帮助......我该怎么办?它给了我错误
  • @vicky : 老兄发布你的整个代码 :) 如果你正在使用 searchDisplayController 尝试按照我提供的教程将你的代码移动到 UISearchController :)
猜你喜欢
  • 2015-06-27
  • 2013-09-04
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
相关资源
最近更新 更多