UISearchBar和UISearchDisplayController

来源:互联网 发布:零基础程序员入门书籍 编辑:程序博客网 时间:2024/05/17 06:53

UISearchBar继承自UIView、UIResponder、NSObject

 

  属性:

 autocapitalizationType————自动对输入文本对象进行大小写设置(包含4种类型,但是有时候键盘会屏蔽此属性)

 autocorrectionType————自动对输入文本对象进行纠错。

 backgroundImage————searchbar的背景图片,如果图片不是可伸缩的或者1点宽,则一般被平铺

 barStyle————控件的样式

 delegate————控件的委托,委托要遵从UISearchBarDelegate协议,默认是nil

 keyboardType————输入时,键盘的样式

 placeholder————半透明的提示文字,输入搜索内容消失

 prompt————显示在控件顶部的一行提示文字

 text————控件上面的显示的文字
 showsBookmarkButton————是否在控件的右端显示一个书的按钮(输入文字时消失)
 showsCancelButton————是否显示cancel按钮(默认是显示)
 showsSearchResultsButton————是否在控件的右端显示搜索结果按钮
 searchResultsButtonSelected————搜索结果按钮是否被选中
 tintColor————bar的颜色(具有渐变效果)
 translucent————指定控件是否会有透视效果
 scopeButtonTitles————搜索栏下部的选择栏,数组里面的内容是按钮的标题
 selectedScopeButtonIndex————搜索栏下部的选择栏按钮的个数
 showsScopeBar————控制搜索栏下部的选择栏是否显示出来(需设置为YES 才能使用scopebar)

 

UISearchBar不执行搜索行为,必须使用delegate,当输入搜索文本、点击button按钮后,代理的方法会完成搜索对应的操作。

 

1.编辑输入事件:

  • – searchBar:textDidChange:  

  • – searchBar:shouldChangeTextInRange:replacementText:

  • – searchBarShouldBeginEditing:

  • – searchBarTextDidBeginEditing:

  • – searchBarShouldEndEditing:

  • – searchBarTextDidEndEditing:

2.点击按钮事件:

  • – searchBarBookmarkButtonClicked:

  • – searchBarCancelButtonClicked:

  • – searchBarSearchButtonClicked:

  • – searchBarResultsListButtonClicked:

3.Scope按钮事件:

  • – searchBar:selectedScopeButtonIndexDidChange:

 

利用UISearchBar的委托事件 textDidChange, 当在搜索框中输入完成后,如果输入的文本长度>0,可以调用自己的搜索方法,得到搜索结果,然后再reloadData,刷新一下。如果输入文本长 度<0,则需要恢复到原始数据。这个方法可以在边输入搜索文本边显示结果。 如果需要按“search”按钮再搜索,则将上述操作放在searchBarSearchButtonClicked中。

 

利用UISearchDisplayController可以简化很多操作,也能达到搜索的目的。

属性:

active————是搜索界面可视化,默认为no,可用setActive方法设置.

delegate————委托

searchBar————在searchdisplaycontroller初始化后,searchbar是不可修改的,是readonly属性的.

searchContentController————管理搜索内容的试图控制器,一般是一个UITableViewController的实例,意思是针对一个UITableView的内容进行搜索

searchResultsDataSource————搜索结果的数据源

searchResultsDelegate————搜索结果的委托

searchResultsTableView————搜索结果要展示在哪个tableview中(read-only);

searchResultsTitle————搜索结果视图的title

 

初始化一个searchDisplayController:

 

        UISearchBar theSearchBar [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width-50, 40)] autorelease];

        theSearchBar.placeholder @"enter province name";

        theSearchBar.autocorrectionType UITextAutocorrectionTypeNo;

        theSearchBar.autocapitalizationType UITextAutocapitalizationTypeAllCharacters;  

        theSearchBar.scopeButtonTitles [NSArray arrayWithObjects:@"All",@"A",@"B",@"C",@"D" ,nil];

        theSearchBar.showsScopeBar YES;

        theSearchBar.keyboardType UIKeyboardTypeNamePhonePad;

        theSearchBar.showsBookmarkButton YES;

        

        tableView.tableHeaderView theSearchBar;  //将searchBar添加到tableView的头,注意滚动出屏幕后,搜索框也不在了,只出现在首页

        

      UISearchDisplayControlle* searchdispalyCtrl [[UISearchDisplayControlle alloc] initWithSearchBar:theSearchBar contentsController:self];

        searchdispalyCtrl.active NO;

        searchdispalyCtrl.delegate self;

        searchdispalyCtrl.searchResultsDelegate=self;

        searchdispalyCtrl.searchResultsDataSource self;

 

使用UISearchDisplayDelegate的委托方法进行搜索操作:

1.搜索状态改变:

  • – searchDisplayControllerWillBeginSearch:

  • – searchDisplayControllerDidBeginSearch:

  • – searchDisplayControllerWillEndSearch:

  • – searchDisplayControllerDidEndSearch:

2.装载和卸载tableview:

  • – searchDisplayController:didLoadSearchResultsTableView:

  • – searchDisplayController:willUnloadSearchResultsTableView:

3.显示和隐藏tableview:

  • – searchDisplayController:willShowSearchResultsTableView:

  • – searchDisplayController:didShowSearchResultsTableView:

  • – searchDisplayController:willHideSearchResultsTableView:

  • – searchDisplayController:didHideSearchResultsTableView:

4.搜索条件改变时响应:

  • – searchDisplayController:shouldReloadTableForSearchString:  

  • – searchDisplayController:shouldReloadTableForSearchScope:

 

searchDisplayController 自身有一个searchResultsTableView,所以在执行操作的时候首先要判断是否是搜索结果的tableView,如果是显示的就是搜索结 果的数据,如果不是,是TableView自身的view,则需要显示原始数据。

  if(tableView == self.searchDisplayController.searchResultsTableView)

    {

        arr [self.filterContent valueForKey:key]; //搜索结果

    }

    else 

    {

        arr [self.localresource valueForKey:key];  //原始数据

    }

 

这样就不需要每次都realoadData了。


原创粉丝点击