This chapter continues to talk about the implementation of the search function based on the previous chapter. First of all, the search function after iOS8 has a large change, so my example only support iOS8 later examples. OK, let's get started. First of all, let's declare two global variables first. One is our search box, and one is the filter result. And let our class implement the Uisearchresultsupdating interface.
Then initialize the search box in the Viewdidload function
This piece of code is well understood. The only thing that needs to be explained is the Dimsbackgroundduringpresentation property. The purpose of this property is that when we click on the search box, our tableview is shaded, because we do not want this. So set to False.
Then we implement the Uisearchresultsupdating interface that we just inherited. This interface is relatively simple, just a method Updatesearchresultsforsearchcontroller (Searchcontroller:uisearchcontroller). This function is triggered when the search box is activated, canceled, and the contents of the search box are changed. Look at the code. The logic is simple, which is to modify the data source of the search results based on the search content. Then refresh the table. Here is a special syntax that is closed. In fact, there are similar grammars in. Net. Everyone should be able to understand.
Then is the interface display part, in order to look at the effect, we need to click on the search box to give an initial interface, show what he can search the scope ah, the previous used keywords Ah, what. The TableView prototype cells were modified to 2. Add a cell. The general effect looks like this: I just modeled a hint and a horizontal line to search for more content. The following content icon because I could not find the appropriate resources I did not add, interested students can add a bit.
Then make some changes to the background. Use the initialization interface when the search box is active and the search keyword is empty, setting the row height to 400
Then is the row number and the display processing, basically is distinguishes 3 kinds of states, normal display, just enters the search box, and enters the search key word after
OK, finally look at the effect. This is the effect on the real machine. Initialization
After clicking on the search box
Enter search results
Code Address: Https://github.com/xing98218/FirstClass
iOS Development Series-uitablecontroller-2