2013年1月14日 星期一

UISearchBar-搜尋列整理

無搜尋結果顯示

image

代理:<UISearchBarDelegate>

@protocol UISearchBarDelegate <NSObject>

@optional

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;                      // return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;                     // called when text starts editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;                        // return NO to not resign first responder
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;                       // called when text ends editing
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;   // called when text changes (including clear)
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0); // called before text changes

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;                     // called when keyboard search button pressed
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;                   // called when bookmark button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar;                    // called when cancel button pressed
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2); // called when search results button pressed

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0);

@end

帶有搜尋結果顯示

 image

代理:<UITableViewDataSource, UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>

@protocol UISearchDisplayDelegate <NSObject>

@optional

// when we start/end showing the search UI
- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller;
- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller;
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller;
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller;

// called when the table is created destroyed, shown or hidden. configure as necessary.
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView;
- (void)searchDisplayController:(UISearchDisplayController *)controller willUnloadSearchResultsTableView:(UITableView *)tableView;

// called when table is shown/hidden
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView;
- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView;
- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView;
- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView;

// return YES to reload table. called when search string/option changes. convenience methods on top UISearchBar delegate methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString;
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption;

@end

 

#以下範例為使用UISearchDisplayDelegate代理的方法

搜尋元件初始化:

無scope的設定方式

    searchData = [[NSMutableArray alloc] init];
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.tintColor=bgColor;
    jobList.backgroundColor=bgColor;
    //searchBar.tintColor=[UIColor1 greenColor];
    // Do any additional setup after loading the view.
    /*the search bar widht must be > 1, the height must be at least 44
     (the real size of the search bar)*/
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    /*contents controller is the UITableViewController, this let you to reuse
     the same TableViewController Delegate method used for the main table.*/
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate=self;
    //set the delegate = self. Previously declared in ViewController.h
    self.jobList.tableHeaderView = searchBar; //this line add the searchBar

引用代理方法:

//-----------------------------------搜尋----------------------------------------
-(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    //載入搜尋的table,設定cell背景色
    tableView.backgroundColor=[UIColor colorWithRed:(222.0f/255.0f) green:(255.0f/255.0f) blue:(200.0f/255.0f) alpha:1];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{

    [searchData removeAllObjects];
    jobBas *jobbas;
    for(jobbas in tablearr) //take the n group (eg. group1, group2, group3)
        //in the original data
    {
        NSLog(@"%@:%@",jobbas.jobReason,searchString);
        NSRange range = [jobbas.jobReason rangeOfString:searchString options:NSCaseInsensitiveSearch];
        if (range.length > 0) { //if the substring match
            [searchData addObject:jobbas]; //add the element to group
        }
    }
    return YES;
}

 

 

有scope的設定方式

searchData = [[NSMutableArray alloc] init];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    searchBar.scopeButtonTitles=[NSArray arrayWithObjects:@"全部",@"加點",@"扣點", nil];

    searchBar.tintColor=jobList.backgroundColor;

    //searchBar.tintColor=[UIColor greenColor];

    // Do any additional setup after loading the view.

    /*the search bar widht must be > 1, the height must be at least 44

     (the real size of the search bar)*/

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

    /*contents controller is the UITableViewController, this let you to reuse

     the same TableViewController Delegate method used for the main table.*/

    searchDisplayController.delegate = self;

    searchDisplayController.searchResultsDataSource = self;

    searchDisplayController.searchResultsDelegate=self;

    //set the delegate = self. Previously declared in ViewController.h

    for (id subview in searchDisplayController.searchBar.subviews )

    {

        if([subview isMemberOfClass:[UISegmentedControl class]])

        {

            UISegmentedControl *scopeBar=(UISegmentedControl *) subview;

            [scopeBar setSegmentedControlStyle:UISegmentedControlStyleBordered];

            [scopeBar setTintColor: [UIColor greenColor]];//you can also set RGB color here

            //scopeBar.tintColor =  [UIColor blackColor];

        }

    }

    self.jobList.tableHeaderView = searchBar; //this line add the searchBar

引用代理方法:

//-----------------------------------搜尋----------------------------------------

-(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    //載入搜尋的table,設定cell背景色
    tableView.backgroundColor=[UIColor colorWithRed:(222.0f/255.0f) green:(255.0f/255.0f) blue:(200.0f/255.0f) alpha:1];
}

//
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    /*
     Update the filtered array based on the search text and scope.
     */
    [searchData removeAllObjects]; // First clear the filtered array.
    /*
     Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
     */
    for (jobBas *jobbas in tablearr)
    {
        NSString *wscope;
        if (jobbas.is_GoodJob) {
            wscope=@"加點";
        }else wscope=@"扣點";
        if ([scope isEqualToString:@"全部"] || [wscope isEqualToString:scope])
        {
            NSComparisonResult result = [jobbas.jobReason compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
            {
                [searchData addObject:jobbas];
            }
        }
    }
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

 

   

Loading...