Welcome back.
Table view are actually quite complicated.
They do a lot of different things and they benefit from so many different outlets.
So to get us started I'm actually going to show you how to use a table view controller which minimizes
the amount of code I have to program and that allows me to show you the visual aspects of a table view
easier.
So let's going to export an index score I'm going to start a new application.
It is off to type single view as usually it is.
And I'm gonna call this one table view controller table view controller app and then once that's safe
I'm going to head into my application and go to the storyboard and in this story but I don't want to
have the normal regular view controller.
I want to delete that.
Instead I want to have a table view controller which is this guy up here.
And if you show it with the text because he has called a table view controller so what a table of your
countries is it especially kind of view controller that built into it has a table view that is that
object in here a table view and then between the table view it has a set which is called a table view
set and we'll get to see how to build these things on our own.
Really really soon.
But for now because I want to speed up the process we're going to use a table view controller that built
into it has the table view and the set.
So the first thing I have to do in here I have to select my table view controller and say it is my initial
view controller so select not appear I'm going to say it is the initial view controller.
So the little arrow here shows up and shows me that when the applauds we will see this view controller
now.
The next thing I have to do is I have to assign a class for this table view controller and if I go up
there in the classes it says you have to give me a class of the type you a table view controller and
that's something I still don't have.
So let's go ahead in here I'm going to delete the view controller because we don't need that anymore.
We don't have a view controller anyway and I'm going to stay command and make a new fighter.
It's a quote each class and this type is UI table view controller.
I'm going to call it my table view controller next when I save it created and now we have a new table
view controller class that they can go to my storyboard and introduce it as the class for my table view
controller outlet someone going here and say it's class is my table view controller.
Now the next thing I want to do is I want to go into the table if you to and explain to you what these
different things are.
There is so much code in here already and most of it we don't just need yet.
So the teacher they do need all these and they get rid of all the comments I don't need a view did Lord.
So I'm going to get rid of that there's is a method here that decides about the number of the sections
in my table view.
That's an optional one because the default value for it is 1.
So I'm going to get rid of that as well.
There is a function here called number of rows in section.
That's actually an important one that we have to maintain.
So I'm gonna keep that one and we'll talk about it.
Wait it's gonna do for us.
Then there is another function here called the cell left for a row at.
That's a very important one.
We have to maintain it someone on comment it.
And that's almost all we have to do for this very first example.
Everything else can go.
So I'm going to delete everybody else.
We really get back to them at some point and I will show you what do they mean and where do we use them.
But for now let's simplify our process.
These are the two functions I keep.
One of them says how many rows.
Do you have in this table view.
The other one says so what are you going to show me in this different rows for each row.
What is the cell that you want to see.
So let's go back in here to what storyboard in the storyboard.
I only have one prototype said let's see what our cells actually have.
I want to change two things in here as we go through the lessons we'll get to know more about these
details on set things in here.
But for now the only two things I want to change are these.
One of them is I want to change the style of this safe table view a cell.
I want to make it basic.
That shows me a title in there that I can change.
And I also like give it an identifier which is something that we must to do.
So I'm going to give an identifier called Cell I.D. you can call it essentially anything as long as
you remember it later on.
The reason there needs to be an identifier is because these are prototype cells and you may not necessarily
have only one type of them.
Imagine you're developing an app similar to a Twitter application.
Some of your cells.
They have images and that the text and the tweet.
Some of your cells they only have the tweet.
So these are two different types of cells but they're all living inside the same table view.
So because of that you have to name your cells or prototype cells.
If you have only one.
It doesn't matter what you call it.
I'm going to call it Cell I.D. but if you have more than one then you have to properly name them to
whatever you need them.
So let's go back to our view controller or table view controller.
And in here I have to decide the mod Hall.
My cell is going I look like so we could do a lot of different set things in here but they want to go
really slow and I want to go one step at a time.
The first thing I'm going to do is I'm going to say the number of the rows in this section of this table
view is five.
So there are five rows.
Then in this cell for each of these rows how that cell is going to look like I'm going to say make a
cell.
This is the cueing the reusable cell with identifier that is called The Cell I.D. because that's the
identifier I made myself in my a storyboard.
If you remember hearing this storyboard that cell is as essentially called on and selected again it's
called The Cell I.D..
So that's one thing.
So I have this set I found this cell for each of the rows.
And the only thing I'm going to do for this starting point I'm going to say Sen. Dodd bygone color becomes
you eye color dark red.
So all that should happen is that we have to see a table view with five cells that each cell is essentially
red.
And if that works it means you're on the right track.
And that's exactly what happened.
We have five cells.
Each cell has a default title.
And then there is a little separator between them.
I don't know if you can't see them in here but you definitely can't see them on your own simulators.
So now that we managed to have five says let's go ahead and have them a little bit more organized and
a better way of doing things essentially.
Going to come up here in my class and I'm going to go ahead and say I have a let let's call it set his
array which is going to be Vancouver New York real New Delhi and Washington if that's how you say it
I'm hoping so then I'm going to say for the table views the number of heroes are actually citizenry
does count and then I'm going to go ahead and say I want to see each of these names somebody in the
set.
Now the default says they have a text label built into them simply because we can see that little thing
that says title so I could actually use that.
I'm going to go ahead and say Sell dot text label obviously that say you a label.
As it says in here you own label dot text the value of it is City's array and I have to find the right
index from my array and the index pretty much depends on the location of the cell.
So for instance if the cell is here in the very first one we have to find Vancouver the second one we
have to find new york.
So we have to find the varied this cell is.
And those information are here within an index part an index part is essentially a type of value that
provides us the location of a cell in terms of both the location in the role which are or is it.
Currently this is rule one or two or three or four or five as well as of each section it is.
I currently only have one section so they're all on Section Zero.
But if you had more than that and you will see that in the future then index it will know that you are
in a different section.
So for now I'm going to say citizenry index part doctor.
So I'm going to say find a value from the city's array based on the role of the cell you are trying
to produce and place it in the label of that cell.
Let's go ahead and run this application and see if we get that if it all works we should be able to
see are the names of the five cities appear on five different sounds and it's happening exactly the
way we were hoping it will happen now I want to do one more test in here really quickly let me fix that
there's a little space before the name of the real I want to do this I want to say every cell not background
color becomes UI color dot on light gray.
So that's one thing they all become light gray if index paths dot roll modulus two is zero.
So essentially that even numbers do this cell that background color becomes you eye color to white.
So essentially I'm saying that even colors make them white.
Yeah all the numbers make them light gray.
You could of course write them if else but I find this an easier way to set everyone to something and
then change the others.
Now let's go ahead and run the application again.
So what's the application load this time.
Now we have all the cells some cells are being shown in a white back on some in a light gray back on
which is exactly what you were hoping for.
Let's quickly recap everything we did in this lesson.
This was an introduction to a table view controller.
We made a table to view controller in the storyboard for its prototype cell.
If we made sure that it has an I.D. and identifying and features a set and a state for it.
Put this side to be basic and then we went into our table view controller class and we said we need
only two functions number of rows in section and sell for row at index spot for the number of rows.
We said they are there as many as we have objects in our array.
And for the Center for right index math we introduce a new value called cell which is of the type cell
I.D. and then we customize it's different you know attributes.
We gave it a text label changes make on color.
The only thing that might be a little bit difficult is this one method.
What is the Q reusable cell.
So these cells in here essentially are called reusable simply because if they go outside the screen
they get reallocated to the bottom of the table view.
And if you're scrolling in the opposite direction if they go outside at the bottom of the screen they
get reallocated to be used somewhere on top of the table view.
This is all a table view deep cues or gets rid of its different cells and is able to use the reusable
says veto out having to allocate so many many cents.
In other words if you had this array to be every city on the planet that would be hundreds of thousands
of defend you know entries in the array but your table view would only show you a handful of them that
the amount that can actually fit within this table view a screen and the rest of them wouldn't allocate
unless they're being viewed you can try to make a table of you using a school view on your own you can
add a label continuously to a scroll view and you will see that as soon as you had a few thousand cells
it actually tries to slow down and perhaps it's going to even crash your application simply because
is going to maintain all of them in its memory which is not a very good idea but table views they manage
that they simplify that process and that's the power of them and the advantage of them.
So that's about how to use a basic table view controller let's keep it up to here and move on to our
next lesson.