IOS -- UISearchBar and UISearchDisplayController, ios -- uisearchbar

Source: Internet
Author: User

IOS -- UISearchBar and UISearchDisplayController, ios -- uisearchbar

UISearchBar inherits from UIView, UIResponder, and NSObject

  • Attribute:

AutocapitalizationType ---- automatically set the case sensitivity of the input text object (including four types, but sometimes the keyboard will block this attribute)

AutocorrectionType ---- automatically corrects input text objects.

BackgroundImage ---- the background image of searchbar. If the image is not scalable or 1 point wide, it is usually tiled.

BarStyle ---- control style

Delegate-controls delegate. The delegate must comply with the UISearchBarDelegate protocol. The default value is nil.

KeyboardType ---- keyboard style when input

Placeholder ---- translucent text, input search content disappears

Prompt ---- displays a line of text at the top of the control

Text ---- text displayed on the Control
ShowsBookmarkButton ---- whether to display the button of a book on the right side of the control (disappears when the text is entered)
ShowsCancelButton ---- whether to display the cancel button (display by default)
ShowsSearchResultsButton ---- whether to display the search result button on the right side of the control
SearchResultsButtonSelected ---- check whether the search result button is selected
TintColor ---- bar color (gradient effect)
Translucent ---- specifies whether the control has a perspective effect
ScopeButtonTitles ---- selection bar at the bottom of the search bar. The content in the array is the button title.
SelectedScopeButtonIndex ---- Number of buttons in the selection bar at the bottom of the search bar
ShowsScopeBar ---- determines whether the selection bar at the bottom of the search bar is displayed (you must set it to YES to use scopebar)

 

UISearchBar does not perform the search action. You must use delegate. After you enter the search text and click the button, the proxy method will complete the search.

1. Edit input events:

  • -SearchBar: textDidChange:

  • -SearchBar: shouldChangeTextInRange: replacementText:

  • -SearchBarShouldBeginEditing:

  • -SearchBarTextDidBeginEditing:

  • -SearchBarShouldEndEditing:

  • -SearchBarTextDidEndEditing:

2. click the button event:

  • -SearchBarBookmarkButtonClicked:

  • -SearchBarCancelButtonClicked:

  • -SearchBarSearchButtonClicked:

  • -SearchBarResultsListButtonClicked:

3. Scope button event:

  • -SearchBar: selectedScopeButtonIndexDidChange:

Use the delegate event textDidChange of UISearchBar. After the text is entered in the search box, if the input text length is greater than 0, you can call your own search method to obtain the search result, and then reloadData, refresh. If the input text length is less than 0, the original data needs to be restored. This method can be used to enter search text and display results. If you need to search again by pressing the "search" button, place the above operations in searchBarSearchButtonClicked.

 

Using UISearchDisplayController can simplify many operations and achieve search purposes.

Attribute:

Active ---- the search interface is visualized. The default value is no and can be set using the setActive method.

Delegate ---- delegate

SearchBar ---- after the searchdisplaycontroller is initialized, The searchbar cannot be modified, and it is the readonly attribute.

SearchContentController ---- the attempt controller to manage the search content. Generally, it is an instance of UITableViewController, which means to search for the content of a UITableView.

SearchResultsDataSource ---- data source of the search result

SearchResultsDelegate ---- delegate the search result

SearchResultsTableView ---- The tableview in which the search result is to be displayed (read-only );

SearchResultsTitle ---- title of the search result View

 

Initialize a 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; // Add the searchBar to the tableView header. After scrolling out of the screen, the search box is no longer displayed. It only appears on the homepage UISearchDisplayController * searchdispalyCtrl = [[UISearchDisplayController alloc] initwithsearch: theSearchBar contentsController: self]; searchdispalyCtrl. active = NO; searchdispalyCtrl. delegate = self; searchdispalyCtrl. searchResultsDelegate = self; searchdispalyCtrl. searchResultsDataSource = self;

 

Use the delegate method of UISearchDisplayDelegate to perform the search operation:

1. The search status changes:

  • -SearchDisplayControllerWillBeginSearch:

  • -SearchDisplayControllerDidBeginSearch:

  • -SearchDisplayControllerWillEndSearch:

  • -SearchDisplayControllerDidEndSearch:

2. Load and uninstall tableview:

  • -SearchDisplayController: didLoadSearchResultsTableView:

  • -SearchDisplayController: willUnloadSearchResultsTableView:

3. Show and hide tableview:

  • -SearchDisplayController: willShowSearchResultsTableView:

  • -SearchDisplayController: didShowSearchResultsTableView:

  • -SearchDisplayController: willHideSearchResultsTableView:

  • -SearchDisplayController: didHideSearchResultsTableView:

4. response when search conditions change:

  • -SearchDisplayController: shouldReloadTableForSearchString:

  • -SearchDisplayController: shouldReloadTableForSearchScope:

 

SearchDisplayController itself has a searchResultsTableView. Therefore, when performing an operation, you must first determine whether it is the tableView of the search result. If it is displayed, It is the data of the search result. If it is not, is the view of TableView, You need to display the original data.

If (tableView = self. searchDisplayController. searchResultsTableView) {arr = [self. filterContent valueForKey: key]; // search result} else {arr = [self. localresource valueForKey: key]; // raw data}

 

In this way, you do not need to realoadData every time.

 

A good example can be referred to: http://developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html # // apple_ref/doc/uid/DTS40007848

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.