Swift if I prides itself for being able to allow developers to do things in a declarative way but also
in a more simplified way.
When creating user interfaces.
Now one of the things if you are a mobile developer in general but specifically in Iowa's developer
is that there is this thing called table view table view is essentially a way in which we can create
cells on a list format so we can scroll up and down to show more information right.
So imagine you are acquiring a database of some sort.
That has a lot of data.
So the best way to display that data would be to create rows in a list and so people can scroll up and
down so we can see that information and also be able to tap on specific row and be taken to a details
page per say.
So in Swifty y Of course there is a better and faster way of doing this.
So I'm going to show you right now how to put together a list or a table view for those of us coming
from Objective C and you like it.
Let me create a variable here called names.
Just gonna be an array that will contain a few names here.
So I'm gonna type a few names here right here.
All right.
That's simple enough.
So have an array containing a few names there.
So we are going to put all those in a list going to get rid of this.
And of course we have in the actual component that we can use called list like this.
You can see at the bottom here it's a container that presents rows of data arranged in the single column.
So that's very handy.
And we can go straight and pass in the data source as follows.
So inside the parenthesis here I'm gonna just say self that name just invoke our array names that is
and then what I'm gonna do here have to parse the I.D. because I want to make sure that each time we
loop through and create this list here we have to pass this specific idea of each item.
So in this case here it's like what we saw before with for each.
We just gonna say dot self like this game because just a simple array and that should be fine.
And then we'll say name in and of course again pass another view here this text which I mean I just
add the name that we are passing.
So in this case Ryan this we should see something very awesome here that was giving for free.
Look at this just like that we have the names showing in a very nice list here.
Now if if you've done some Iowa's development with objective c and or are you like hit Swifty you are
you'll notice that to create a table of viewers.
This is called at least.
It's pretty cumbersome so you have to put together the actually view and then connected the rows and
cells and figure out which item is being shown and so forth so just like that we're able to create a
list and just pass in the data and then display the list as quickly as possible.
Pretty neat.
And of course as you know we can go ahead and add a few modifiers here when I make bold I say font to
be maybe caption and so forth.
So all of that stuff that we've learned we can of course apply to our textbook.
That's a little bit too small but you get the point.
Now our names array here is very simplistic.
Usually we want to create something a little bit more sophisticated per say so it could come down here
and create a struct and a car call this country which will represent a single country.
And of course usually when you create data types you want to put those inside of their own data folder.
But in this case because we're just trying to get things across here it's OK to create a struct here.
Sometimes it's actually necessary to do so.
So in this case here I can say for instance let name be stuff String let population be of type and then
when I say left I.D. it's going to be of type account.
Right.
So we're going to go in and create an actual data type of type country so that we can populate everything.
So at the top here we can say let country list of course you're going to be of type country objects
or types.
There we go.
And I'm going to go ahead and create the actual array because since we're here might as well do that
I'm going to fill out this array with country.
So in this case my passing I.D. start with zero and string let's say Mozambique population we're going
to just put a gibberish number here doesn't really matter.
And we're the same thing for others.
I already have a list of countries so I'm gonna just copy them and paste them here to simplify our life.
So now we have our country list and we're going to use that.
Now one thing to remember here is that this country type that we created at the bottom here we need
to actually make it confirmed to identifiable so identifiable as such.
The reason why is because with our list if we don't add this as identifiable then we're gonna have some
issues looping through and adding those items or these countries in our table for you list there.
So at this point here we'll just change a few things so instead of call these names gonna call this
country list as say country list as such and instead sort of self I can just go ahead and actually get
the I.D. because each country has its own I.D. And we're going to use that.
OK.
Now we have a problem here because says requires that a country conforms to string.
What he's saying here is that this name as it is right now name is an actual object of country.
So we need to go and invoke the property name or whatever else we want do.
This gets a lot changes from name to country to be more specific.
Which means I also have to of course change the handler there.
So a country like this.
All right.
So save this.
Give it a run.
We should be able to see the list of at least 10 countries.
And while I can see it's looking real good.
OK.
So just like that we're able to create a very simplistic list from first to having just a simple array
and then creating an actual country list which contains objects of countries which contain these names.
And of course population.
So in the next video what are we gonna be doing is we're going to make sure that we add in navigation
to our list here.
So when we click on one row you should take us to somewhere else or a detailed space.