They come back again in this this and I will show you two different ways of running a search within
a table view in the first method they will show you how to filter the content of the table view.
So he chose only doors that you're interested in and the other method able to show you how to add a
search display result controller that shows you a table view on top of any view that shows you the results
that you're asking for.
So kind of a full on lesson and let's get this started.
I'm going to go to export.
I'm going to start a new application.
I'm going to call it search and table view and in here once I have everything I'm going to head into
my storyboard.
And this time I'm not going to use a table view controller.
I'm actually going to use a table view.
So we're going yeah I'd say I have a table view which is this guy I'm going to place it here.
I take that like this then I'm going to go ahead and add a table view cell which I need for my table
view right when I'm here.
I'm gonna go ahead for my cell and say it's the style as basic as identifier is cell I.D..
I will also add a new search bar.
So a search bar I'm gonna add in here and I could actually select both the search bar and the table
view go down in here and say add the missing constraints.
This doesn't to my opinion are the best set of constraints but for my purpose it actually does the work.
The next thing I want to do in my storyboard is setting the table view delegate and data source to be
on my view controller.
I want to hold control and drive over a year say data source do the same thing we delegate.
So now both of them are set.
And finally I'm going to go to my assistant window get myself some better space in here and say this
is actually my search bar.
It is of the type you I search for Connect.
And of course this one is my table view.
So my table view all right.
So so far we haven't done anything that is new.
But now let's go ahead into a view controller and I'm gonna assume that my table view shows me the names
of some cities.
And every time that they search in my search what it filters them based on what I'm asking for it.
So I'm really going here and say let the cities array be and this time I'm going to have to add a few
more cities.
So I'm going to say New York maybe Tokyo maybe thrown all
over and say the same genus that's going to spend it a whole Chicago reel.
So I have some cities in here in my city is array and then but I'm going to have to do is I'm going
to have to add the two protocols in here.
So I'm going to say I need a new table view a data source and I need a you a table view delegate.
And if you remember there are two methods that we must conform to them.
I'm going to add both of those methods.
One of them is called number of rows in sections and the other one is going to be set for raw at index
spot.
So these are the same things that we have been doing over and over and over in this section.
That number of rows is going to be citizenry that count.
I have to of course return that.
And then for my cell I'm going to say let's sell to be table view that the Q reusing cell I.D. Let me
get myself into the space in here and then go ahead and say cell that next level.
That text becomes the citizenry object that index that's raw and return.
That said so by now we should have a table view that shows us these these objects and it's telling me
Are you sure this thing exists.
I know this it is this or less force on it and we don't need to have that option optional that anymore.
Let me run the application just to get some confidence that we're in the right track by now we should
be able to see everything that we have done before except that this time we have a little more you know
objects in our table view and if it all works we should see all of these.
Now the next call is tapping in here typing something in the search and we want these guys to filter
based on the search criteria.
So let's go ahead in here.
So for doing the search I'm going to assume that we have another array that is the array of the filter
of the content and we showed that in our table view.
So essentially and let's go in here and say once we have the citizenry I'm also going to go ahead and
have very of them.
Let's call it filter array which is going to be a String array and is currently empty.
So we have the filter array and we have to use it instead of the city's array.
So I'm going to say everything the table view is going to show its numbers make it based on the filtered
array and everything that you want to show a cell make it based on the filter array but I'm also going
to go ahead into my view did Lord and say right at the very beginning filter array is the same as the
city's array.
So at the beginning there is no search happening just duplicate everything in the city is a place it
in the film territory.
So if you're run the application again we should see exactly the same thing as before we have the array
being populated from the city's array.
And that is being used to show us the items in our table view now that we have that you could actually
go ahead and run the search in here to do that.
I'm going to go ahead and say you may view this.
Lord I'm going to say my search what dot delegate which is of the type you I search for delegate becomes
self since I'm doing that I have to conform to that protocol.
So you I search for delegate.
No they have the delegate.
I could actually use one of its methods.
That is very similar to the text field text changed and that is the way that the very by them that's
going to become text did change.
So it is search for you I search by text.
Did that change each time that the text value and the search bar actually changes if you want to filter
or array based on the criteria that recently has been added there.
So in text did change the first thing I want to do is I want to say filter array becomes a new copy
of the city's array.
I want a start from a fresh copy.
Then when I go ahead and say as the text changes if the text value is not empathy because it's possible
that when the text changes you're passing the backspace and you're deleting everything.
So what we want to make sure that we are not on an empty mode.
So I'm going to say if search bar dot text is empty.
If that is not the case then let's go ahead and filter our array.
So I'm going to say if that wasn't the case and we have to make sure that this is a non optional value
so I could essentially allow it to autocomplete.
So is gonna make that the entire thing the search for exists the text to exist and is not empathy so
is a force on a loved one.
So in that I'm going to say if that was the case if there is some text visible in our search for text
then here's what you want to do.
You want to say filter array becomes city's array dot filter.
And now we have to filter it using a certain closure and the closure I want to use is this I want to
say there is something called N3 and free of the type a string.
And here is how I want to return it because it has to return a boolean value.
So in case this filtering is correct for this particular entry it gets added to the filter array.
What is the condition for that.
I'm gonna say the condition for that is this return entry does contains if it does contain the value
from search bar dot text.
So for instance if your entry value is this one New York and here you have a value that you have enter
such as n letter N if that n exists it contains with an entry that returns as true and since it returns
through through the filter it gets added to the filter array otherwise it doesn't.
Once that entire process is done I'm going to go ahead and say s that my table view that the reload
data because now we have a new filter array.
The square then run this application.
So now we have our table view and we have our search bar.
And if I type in it actually filters it based on doors.
If I type t makes it based on those guys I want to show you one other thing that we could do in here.
If I type for instance n I get the right thing but if I type for instance e it doesn't show me the right
thing even though I have characters in here that they have lower case e and anytime that you're running
a search is actually better if you eliminate the capitalization.
So to do that I'm going to go ahead and say entry dart to lower case if it that contains whatever we
have in the text got to lower cased and that's because the lower case version of letters might be identical
to each other for instance.
All of these letters e or E are exactly the same or I or I.
But if we were comparing them against each other they would be different.
But if we compare them lower case they both become E N E and the other ones they become I and I and
so on and so forth.
So let me read on the application and now we should be able to type any combination of capital or lowercase
letters and we should still be able to filter them.
So in here I'm going to say New York perfect as well as a capital E which is still gets me all the results
that they do have an Eevee team them and that's pretty much you're doing a search of it in a table view
and that's going to get us there.
Now this was doing a search v Tina table view that we already have a storyboard.
Now what I'm going to do is I'm going to make a new application where we don't actually have a table
view but we have a list of items and we want to be able to filter through those items and represents
them in a table view.
So in the second iteration we don't actually have a table view in our application.
What we have is perhaps a map or just a view controller that has some items.
But do we know that our user has access to a set of different items and what we want to be able to do
is we want to be able to search for items based on a string of text and we endorse the ones that they
match our criteria.
We want to see them in a table.
So to do that I want to go ahead and start a new application.
I'm going to call this application search controller app simply because in this application we benefit
from something that's called a search controller.
So what's happening in here is this I have a view controller in my storyboard that could have essentially
anything in it.
The one thing they really need to have is a search bar in it and I'm not gonna make the search part
of it in this story what I'm rather going to make it programmatically but they need to assign a container
of you for it.
So I'm going to go in here and say I need a UI view and this UI view and when a place it somewhere up
here you can obviously set all the constraints on what not.
But for me that's all I want to do I want to place it in here and say it has a light gray bank on.
This is where my search bar is going to be and I'm going to head back to my assistant window in here.
I'm going to make a an outlet for this guy.
Say this is search bar container view.
So that's all I want to do in my storyboard assuming that you will have other things in your story but
most likely you will have a map or anything of the sort that can have so many different items that you
can search within them.
So I'm going to head back to my view controller and in here I'm going to go ahead and say Let me get
better formatting first.
And the first thing I want to do is this I want to say I have a value event and I'm going to call it
the search controller which is of the type you I search controller.
Now a search controller is essentially a type of outlet that has a search bar as well as a result view
controller.
So it shows you some results after he does the search.
And since I want to see the results in a table view controller I need another outlet which is of the
type table view controller to do that.
I'm actually going to go ahead and make making you find I'm gonna say command and I'm going to say this
is after type you a table of your controller.
I'm going to call it results table view controller
now now this is a table of your controller that doesn't have a storyboard outlet associated with it.
We will initialize this programmatically but we still need to do everything that we usually do for a
table view.
So to do that I'm let go ahead and say Here I have something called value but unless call it the results
array which is in this case for what I'm doing is after type a string and is empty right at the very
beginning so there's nothing in it.
At the very very beginning the number of sections I'm actually going to remove for the number of the
rows I'm going to say it is actually a result array does count.
And then the only other method I necessarily must have is the cell for rows.
I'm going to say that one that we get rid of everything else for that one I'm going to say I have a
cell that said is using cell I.D. and then I'm going to say cell not text labor.
That text becomes results array index part.
That's right.
Now you might be wondering is the cell I.D. coming from because we don't have anything for the results
table view controller in our a storyboard and that's a very valid question.
We have to essentially register a cell to be used for this table if you control it and to do that.
I'm going to go ahead and say I first need to make myself a table view said so I'm going to start new
unified command and go to a view user interface type and this one I'm going to essentially call it the
result cell.
And as you can imagine in here I don't need a view but they do need is a UI table view cell essentially.
So I need one of these guys and this is the cell I want to use for every resource that they find this
cell that they have.
I'm going to call it to be cell I.D. for its identifier and I'm going to go ahead and say its type is
essentially basic type.
That's all I have to do in terms of the most basic stuff you need to do to have a new cell.
Now the next thing and the more important thing is you have to go to your resource table view controller
in your view did Lord you have to register it.
You essentially have to say this table view will use this kind of cell and we do it like this.
I'm going to say let me but to be you I never.
And then after type nib name is going to be this name resolved cell.
And we don't have a bundle for it.
So we got the net we got access to this file and then I'm going to go ahead and say self dots table
view dot register in the register I'm going to say register this guy this number for identifier sell
I.D..
So what this means is they have a resolved cell outlet that they want to register as one of their prototype
cells for my table view.
So it would be very similar to you dragging in your table you sell to your table view controller and
each time you do it you add a new I.D. for it and it essentially becomes of one of the prototype cells
that you have access to.
And now since it is registered as viewed it Lord we have access to it because obviously use it in here.
So by now we have a table view that has a results array has the right cell.
And if we can call it somebody it can show us the results that we want but now we have to essentially
use these results table view controller to do that.
I'm going to go to my view controller and I'm going to initialize my results.
Controller of the type results table view controller.
So I have a new variable of that type and then than I had in my view did law.
The first thing I'm gonna do is I'm going to say my results.
Controller becomes a results table view controller and that gets allocated and initialize.
Then I'm gonna go ahead and say this guy my search controller becomes a UI search controller.
I'm one of the initialization is is initialize with a certain view controller.
So I'm going to say my search controller values my result controller which is a table view controller
has its results controller.
So once it loads is gonna show me the results in here in this kind of view controller you could theoretically
use any kind of viewpoint on in there but most often what they have seen is people use a table view
controller because you kind of want to be able to see the results basically by sellers or by your rows.
You could use a collection you can tour as well that is something that would be the study in this section
but most likely you want to use a table view controller in here.
So once they have that the next thing I have to do is this I have to say my search bar container view
over I will see the search will have a sub view and that sub view is the search controller not search
bar.
So I want to add the search bar from my search controller to be between my search container review.
If you don't recall a search container of you was this little you review that they made in here so I
can embed my search bar of it in here.
The next thing I'm going to do I'm gonna say let's set the delegates so search controller.
And when I say DOD delegate shows off the type you a search controller delegate becomes self.
So I'm going to have to add that so that's one.
So let's go ahead in here and say you I search controller delegate that's added.
Let me reformat everything you a search controller delegate gets rid of this extra spaces and the next
thing I want to add is another delegation here which is the search result updated.
So I'm gonna say search controller that search result update her.
It wants to update it in here.
So the updates have to appear from some sort of a function in here.
And the moment we do that is obviously going to tell us you need to embed UI search results updating.
And then add that one and that delegate has a required method which is essentially the update search
for results.
So it says update search results for this UI search controller.
And in saying so what do you want to happen every time that we update.
I'm going to write it down first.
I want to filter my guidance and I want to reload the table view from the results view controller.
I want to present the results view controller.
So a lot of things that we have to do the very first thing we have to do is we have to feel there are
items but they don't actually have any items in here because we don't have any array of items in a real
life application.
This would be something that comes to you from a network that is something we will see in a few sections
from now or you will have some static values.
So I'm actually going to go ahead and copy the values from my previous application.
I'm going to say these are my objects so I have the same cities again.
So I want to filter the same guys once they filter them.
I want to reload the table view from the results view controller based on these filter values.
Want to go ahead and say my results view controller dot array.
I don't remember what they called the array results array that becomes the city's array unless filter
is the filtering is going to happen exactly like what we did last time in the filter.
I'm going to give it a string.
I'm going to call it N3 which is after type a string and I'm going to go ahead and say return if entry
dog contains what do I want to get from the contain.
I want to get these search controller dot search bar dot text so essentially for any item in the city's
array if they have any part of this search bar text within them.
If they contain that return that has a true value and add them to the results array of their results
controller.
Now finally I have to say once that is done my results controller day table view died the reload your
data.
The last thing they have mentioned is present the results of your controller.
But that happens automatically.
The moment that the value in this area is not to nil which means the resource you control has something
to show it will automatically appear.
So we don't really need to worry about that but somebody gets rid of all of these comments.
And now the only thing I should change in here and that's something we saw last time.
Maybe I want to make it lower cased in here and also for the text I want to make it law.
So essentially every combination of the characters would work against each other so empty lower case
contains searched by text lawyer case.
Now if you're on this application we should be able to see a search bar in our container view and if
you search on it a new table view should appear somewhere going here and say an eye presents the ones
that they have an end within them and I nothing.
And e only one of them.
So on and so forth.
If we don't have any of them we obviously don't see anything about the table view simply because this
view wasn't a table view.
If I go back in here to my storyboard I could actually show you that here the background in here it
could be essentially anything.
You could have a maybe a text the text to a large text view that is in here and I'm actually going to
set the constraints for it's all vertical spacing and leading training and the by the mass spacing and
where they could do is I could make its make on color different.
So this is a view controller that is doing its own thing.
Maybe you have some information on it.
Maybe you have a map on it or any other kind of object but then you also have a set of items that you
want to be able to filter through them.
So I could go in here and say new.
And then if I get New York maybe you want to do something there's been a tap on that.
That one essentially appears in here or anything of the sorts that you can simply imagine to do.
So that was a quick introduction of how to use search within a table view or how to use search using
a search controller which allows for both the search and results of your controller to be presented.
Let's keep it up to here and I'm going to move on to our next lesson.