iOS中的自定义搜索栏

时间:2016-01-07 06:30:23

标签: ios objective-c uitableview uisearchbar

您好我在UISerchbar中使用tableview,但有没有简单的方法可以在searchbar中创建自定义tableview headerview

我已经查看下面的链接,但我不明白。

Custom UISearchBar with UISearchController

How do I use the UISearchBar and UISearchDisplayController

我添加了searchDisplayController :: enter image description here

这是我的searchController代码,

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.definesPresentationContext = YES;
    [self.searchController.searchBar setBarTintColor:[UIColor darkGrayColor]];
    self.tableView.tableHeaderView = self.searchController.searchBar;

我也为UISearchBarDelegate,UISearchResultsUpdating设置了委托,

并实施了他们的方法,

    - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    if (_searchController.isActive && _searchController.searchBar.text.length >0){

        NSPredicate *resultPredicate = [NSPredicate
                                        predicateWithFormat:@"SELF.name contains[cd] %@",
                                        searchText];

        abcd = [sortedEventArray  filteredArrayUsingPredicate:resultPredicate];
        NSLog(@"%@",searchResults);

    }


}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController1
{
    NSString *searchString = searchController1.searchBar.text;
    [self filterContentForSearchText:searchString scope:@"abc"];
    //[self filterContentForSearchText:searchString :@"abc"];
    [self.tableView reloadData];
}

取消按钮事件

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
      abcd=[sortedEventArray mutableCopy];
      [_tableView reloadData];

}



   -(void)delete{


    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest * allMovies = [[NSFetchRequest alloc] init];
    [allMovies setEntity:[NSEntityDescription entityForName:@"Data" inManagedObjectContext:context]];
    [allMovies setIncludesPropertyValues:NO]; //only fetch the managedObjectID

    NSError * error = nil;
    NSArray * movies = [context executeFetchRequest:allMovies error:&error];
    //error handling goes here
    for (NSManagedObject * movie in movies) {
        [context deleteObject:movie];
    }
    NSError *saveError = nil;
    [context save:&saveError];
}

仍然无法正常工作

提前致谢:)

1 个答案:

答案 0 :(得分:0)

有些事情已经完成,但我有两个小错误。

1)从Storyboard中删除搜索栏。

2)添加[self.searchController.searchBar sizeToFit];viewDidLoad

谢谢:))