我应该如何使用searchController

时间:2016-04-29 07:48:28

标签: ios objective-c search controller

我应该如何在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包含[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;

}

@end

1 个答案:

答案 0 :(得分:0)

我相信您已通过将UISearchController作为参数传递给nil来声明searchResultsController。因此,tableView searchResultsController被用作UISearchController

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; } ,以便您可以为他们显示不同的单元格。)

如果是的话,

if (tableView == self.yourSearchController. searchResultsController)

如果你已经实例化了一个不同的tableView控制器并且SearchResultsController提供了UISearchController

,那么我在评论nil中的回答会有效

但是因为您在init UISearchController SearchResultsController中指定了active,所以同一个tableView实例将用作UISearchController。因此,比较将始终是肯定的:)

相反,您可以依赖DECLARE @Anzahl int Select @Anzahl = COUNT(tbl_Kommentare.FID_Typ) AS Zahl FROM tbl_Kommentare WHERE ((tbl_Kommentare.FID_Typ)=99) AND ((tbl_Kommentare.FID_Sprache)=1) IF (@Anzahl = 0) BEGIN INSERT INTO tbl_Kommentare (FID_Typ, FID_Sprache) VALUES (99, 1) END ELSE BEGIN SELECT tbl_Kommentare.FID_Typ FROM tbl_Kommentare WHERE (((tbl_Kommentare.FID_Typ)=99) AND ((tbl_Kommentare.FID_Sprache)=1))) END 的{​​{1}}属性并检查tableView正在使用的上下文并正确处理它:)

希望我明白我的观点:)快乐编码:)