2016-01-07 193 views
-3

您好,我在tableview中使用UISerchbar,但有沒有簡單的方法可以在tableview headerview中創建自定義searchbariOS中的自定義搜索欄

我已經檢查下面的鏈接,但我不明白。

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]; 
} 

提前:)不過它不工作

感謝

+0

我曾在這個很長一段時間,最後決定這將是容易只是放置在一個視圖中的搜索欄右邊的表上方。通過自動佈局,很容易控制定位並使標題始終處於同一位置,IIRC是問題所在。 我放棄了它。祝你好運找到解決方法(我想我當時在ios7上)。 – RegularExpression

回答

0

有萬物已完成,但有是我的兩個小錯誤。

1)從Storyboard中刪除搜索欄。

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

謝謝:))