Try to use Uisearchdisplaycontroller and understanding of Apple's packaging habits for controls

Source: Internet
Author: User

This article reprinted to http://blog.sina.com.cn/s/blog_74e9d98d01019vji.html

In the previously done application, many have "search " This function, most of the cases I use only Uisearchbar and combined with UITableView to show the results of the search, in fact, IOS The SDK already has the own control to help us do these things, this is Uisearchdisplaycontroller, of course, there are some shortcomings of this control, I will be one by one.

First of all, I'll start by talking about the Uisearchdisplaycontroller implementation principle:

uisearchdisplaycontroller  is not a viewcontroller, not by its literal meaning to deceive, in fact its parent class is nsobject   It integrates a uisearchbar object and a uitableview objects , uisearchbar object is responsible for displaying the search input box, The UITableView object is responsible for displaying the results of the search. The caller then implements some uisearchdisplaydelegate to do some custom processing, which protocol, the most important of which is

-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller shouldreloadtableforsearchstring: ( NSString *) SearchString This method gets the user input keyword searchstring, and then the developer processes the search result and returns YES, indicating reload UITableView object again

How to use:

. h file

#import

@interface viewcontroller:uiviewcontroller{

Uisearchdisplaycontroller *searchdisplaycontroller;

Nsarray *_searchresults; An array that holds the search results

}

@property (Retain, nonatomic) Iboutlet Customsearchdisplaycontroller *searchdisplaycontroller;

@property (nonatomic, copy) Nsarray *searchresults;

@end

. m File:

#pragma Mark TableView

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section

{

Nsinteger rows = 0;

if ([TableView IsEqual:self.searchDisplayController.searchResultsTableView]) {//indicates that the current tableView is displaying search results

rows = [Self.searchresults count];

}else{

All the data can be displayed here, the general local search is available to, such as "phone book "

}

return rows;

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

static NSString *cellidentifier = @ "Cell";

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];

if (cell = = nil) {

cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier] Autorelease];

Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator;

}

if ([TableView IsEqual:self.searchDisplayController.searchResultsTableView]) {

Cell.textLabel.text = [Self.searchresults objectAtIndex:indexPath.row];

}else{

Cell.textLabel.text = [Self.allitems objectAtIndex:indexPath.row];

}

return cell;

}

#pragma Mark-uisearchdisplaycontroller delegate Methods

-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller shouldreloadtableforsearchstring: ( NSString *) SearchString {

According to the user's input keyword searchstring, processing the search results (can be local search, can be requested from the server data) to the array

In Self.searchresults

return YES; Returns Yes to reload the TableView object

}

This is how the Uisearchdisplaycontroller control is generally used.

The shortcomings

When using Uisearchdisplaycontroller, do you find that when the keyboard bounces out, the default is to hide the Navagationbar, if you do not need to hide Navagationbar, The best way to handle this is to rewrite the Uisearchdisplaycontroller -(void) SetActive: (bool) Visible animated: (BOOL) Animated method:

First, customize a class Customsearchdisplaycontroller, inherit from Uisearchdisplaycontroller, and then override the method in the. m file and actively display it in the method Navagationbar,

#import "CustomSearchDisplayController.h"

@implementation Customsearchdisplaycontroller

-(void) SetActive: (bool) Visible animated: (BOOL) animated

{

[Super Setactive:visible animated:animated];

[Self.searchContentsController.navigationController Setnavigationbarhidden:no Animated:no];

}

@end

When there is no matching result, the default is to display a "no result" label on the TableView, if you want to customize the label, you can loop through the label in TableView, and then follow your thoughts to set:

-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller shouldreloadtableforsearchstring: ( NSString *) searchstring

{

[Self filtercontentforsearchtext:searchstring];

if ([filteredlistpinyin count] = = 0) {

UITableView *tableview1 = Self.searchDisplayController.searchResultsTableView;

For (UIView *subview in tableview1.subviews) {

if ([subview class] = = [UILabel class]) {

UILabel *lbl = (uilabel*) subview; SV changed to Subview.

Lbl.text = @ "no results ";

}

}

}

Return YES to cause the search Result table view to be reloaded.

return YES;

}

In addition, because the Uisearchbar objects and UITableView objects in Uisearchdisplaycontroller are accessible, it gives us a lot of convenience to change their appearance on the UI at will.

Apple's understanding of the control encapsulation habit:

The control encapsulation that I did before is basically inherited from UIView or Uiviewcontroller, so that the custom controls can be directly add to the view and render after initialization.

But take a closer look at Uisearchdisplaycontroller, he's not an object of a view class, and when I'm suddenly stuck in a no-road situation by calling the initialization function and specifying delegate, I've now created a good

Uisearchdisplaycontroller object, how do i show it to my app's view?

Study for a while found, as long as the uisearchdisplaycontroller Uisearchbar Object Add to the application view is OK! It's very strange why it's implemented in the way it was before its own

Implementation is different, then show the results of the search TableView when it is displayed in the application view? Through Test discovery:

After the original Uisearchdisplaycontroller Uisearchbar object was initialized, some actions were performed by default, as implemented in Uisearchdisplaycontroller Uisearchbar's uisearchbardelegate method,

To detect a series of user behaviors such as "Click Input Box","search keyword Change", and then uisearchdisplaycontroller the "pop-up keyboard" based on these behaviors , "Show search results TableView" and so on, and these responses are

Behavior it does not write itself dead in the class, but by leaving out a delegate to the outside, allowing the outside to implement these specific response behaviors. As to how it adds the result TableView to the app view , it's clear that

Initialization function:-(ID) Initwithsearchbar: (Uisearchbar *) Searchbar Contentscontroller: (Uiviewcontroller *) Viewcontroller; There is a Contentscontroller variable that refers to the application view Congtoller,

That makes sense.

Thus, we can find that Apple's encapsulation of the control is strictly in accordance with the MVC pattern, whereUisearchdisplaycontroller is the model layer, it is not a View, he is not affected by View control (such as add to App view after initialization), he should be to control the view,

It only handles logical data (such as getting the user input keyword and matching the result set), so we can see why the application view is just the Add Searchbar object .

Try to use Uisearchdisplaycontroller and understanding of Apple's packaging habits for controls

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.