hey guys my name is Cal and in today's Swift and Xcode tutorial we are going to be filtering
our table view example based on some filter buttons and also going to be able to search
it by some text in a previous tutorial i showed you guys how to build a table view
so you probably want to have some sort of table view before you get started on
this video if you don't have a table view already implemented go check out the video
there's a link in the description otherwise we're just going to use a ui search controller to search
our list and we're also going to implement some scope buttons which are essentially
just filter buttons so rather than user having to type in some selected things if we already
sort of have a rough idea of what they want to search for or how they want to filter the list
implementing some scope buttons is a great idea as this is a search and filter tutorial in order to
start this tutorial you just want to have a populated table view first thing i'm going to do
is create a navigation controller just drag and drop that in there and the navigation controller
automatically creates a table view we're just going to delete that one because we've already
got one and then i'm going to drag across the storyboard entry point to our navigation
controller and then from our navigation controller i'm going to link it up with our view controller
so i'm just going to right click and drag onto our table view controller and say root view controller
cool and you can see it's given that little bit extra space so basically this is our nav bar
and we can give it a title so i'm just going to call the table view controller i'm going to
call it shapes and i'm going to call the detail one detail next we're going to head back into
our table view controller dot swift file and i'm just going to declare our search controller so i'm
just going to say let search controller equal to ui search controller and then in our view
did load we're going to create a method called edit search controller inside our init search
controller method we're going to well you guessed it we're going to initialize our search controller
i'll highlight all of the ones that are essential there are a few lines here that
you won't even need to implement they're just purely look and feel sort of adjustments so
you know enables return key automatically and ui return key type of done you could just leave those
and you'll get the default here we're assigning navigation item search controller to the search
controller that we just created here we're going to declare our filter buttons so i'm just going
to say search controller dot search bar dot scope button titles is equal to and then we're
just creating an array list of strings and finally i'm going to say our search controller dot search
bar delegate is self so the view controller our current table view controller basically it's the
view controller's responsibility to make sure that the search bar and the search controller works and
as of right now our view controller doesn't do that that's why it's throwing us these errors
so what we're going to do is just click on the error and hit fix for both of those search results
updater and the search by delegate and all that we did there was basically put in these two lines so
our table view controller now implements ui search bar delegate and ui search results updating and as
of right now our table view controller class does not conform to ui search bar delegate protocol
click on the error message and say add protocol stubs and i'm going to put this method down the
bottom heading back up to the top i'm just going to create another array of shapes so
this one's called filtered shapes cool and then in our update search results method i'm just
going to declare our search bar so let's search bar equal to search controller dot search bar
next we're going to declare another variable this is called scope button or it should
really be called selected scope button and then we're just going to say scope button is
equal to search bar dot scope button titles and then search bar dot selected scope button index
so just getting our selected filter essentially and then we're going to say let search text equal
to search bar dot text we've got both of the user inputs so we know what the user has searched for
by their search text and we know which scope button or filter button they've selected
now we need to filter our list based on those parameters so we're going to create a method for
this it receives search text and scope button both of which are strings and the scope button
default is all inside this method we're going to say filtered shapes is equal to shape list
dot filter so go through each of the shapes in the shape list next we're creating a balloon we're
checking to see if the scope button is all or the scope button is equal to our current shape name
next we're going to create an if statement to check if there's something in the search text so
i'm just saying search controller search bar text is not equal to nothing essentially and then we're
going to say let search text match so another boolean and it's going to be true if shape name
to lowercase contains our search text lowercase so just checking to see what's in the search text
if it matches our current shape name we're just going to return if our scope match and our search
text match are both true then this shape should be in our filtered list so we're going to put
our current shape into our filtered shapes list i'm just going to say else and in this scenario
there's nothing we know there's nothing in the search text so we're just going to return our
scope match so if it matches our scope we're just going to reload the data so shape table
view dot reload data next we just need to make some adjustments to our table view protocol stubs
so in our number of rows and section method we're just going to say if search controller is active
return our filtered shapes.count instead of just our shapeless count otherwise return
our shapeless count and in our index path we're just going to create an if statement saying if
our search controller is active get this shape from filtered shapes and otherwise get it from
shape list like we've been doing and we're going to do the exact same thing for our segue
so i'm just going to copy that chunk of code and put it down where we've got selected shape
and then refactor this shape to be selected shape just a quick typo fix up here i spelt
square on and also the other thing is we're going to say shape name . lowercase contains
our scope button lowercase rather than being equal to exactly that's just going to work for our rect
abbreviation scope button or filter button cool but if we go and run this now you can see we've
got the search tab at the top there if you click on that you can filter based on the different
little filter tabs and the segue should work and we can also type in a little bit of text
and you can see that the search controller and the scope buttons all work together
so that was an example of how to search and filter a table view using Swift and Xcode 12.
If you liked this Swift and Xcode tutorial let me know otherwise i'll catch you guys in the next one
cheers