Hi there.
Welcome back.
In this lesson we will see some of the more advanced methods of table use.
So I'm going to go into X scope and I'm going to start a new project.
Call it table view methods and once they get to table view methods I'm going to actually use a table
view controller.
These folks I'm going to do with a little bit faster because I know you guys have done it multiple times
so I don't need the view controller where they would need is an actual table view controller.
And for my table viewpoint I'm also going to add a navigation controller.
So when it start my view with a navigation controller.
So these are the two of them you could keep your table of your controller.
And instead of that one at a navigation controller route view controller for this or you could maintain
a navigation controller that by default comes with a table view controller.
The next thing I'm going to do is I'm going to add a new file a cocoa touch class that is a UI table
view controller.
I'm going to call it my table view controller.
And of course my table view controller becomes the class for my table view.
So my table view becomes my table view controller.
Now we have only one cell in here that cell as always on when I give it an idea and when I call it to
be cell I.D. And I want them to be a little bit taller than usual.
So I'm going to say the raw height is actually sixty fourth.
So I get to see a slightly taller cell on the cells all of the basic type.
So nothing is new in there.
The last thing they have to do is they have to make sure my navigation controller is the initiated view
controller which is gonna be done like that.
So now they want to do is this I want to have two sections in this table view controller and then we're
going to move on with adding some new methods to them.
So I will go into my table view controller and up here I'm going to go ahead and say I actually have
two arrays.
Let's say let's country is array which is coming to be Canada which is where I am right now.
Let's say next form would be us next along with the Mexico next one would be India are like that and
maybe Poland and Brazil.
That's all the countries I can think of right now.
And then I'm going to go ahead and say I have another one which is cities array and that's going to
be Vancouver and from the U.S. We're going to get New York and from Mexico we're going to get Mexico
City I'm from India.
We're going to get New Delhi and from Portland if I'm not drawing we're going to get Warshaw and from
Brazil we're going to get Rio.
So we have two arrays each of them has some items in them.
And the next thing I'm going to do is I'm going to go to the sections number of sections which we usually
delete and we allow it to be the default value.
This time I'm going to say actually return to.
And then what I'm going to do is I'm going to go ahead and say the number of rows in sections.
This rate becomes a little bit different will be if section is zero then you'll have to return countries
array that count and else you have to say return cities array cities cities array that counts.
So depending on the section the number of the rows is different.
One of them is for where the countries are being shown.
One of them is for where the cities are being shown the next thing is sell for raw index fat is going
to be.
Let me format everything nice and tidy.
The sale is called sell I.D. And I'm going to go ahead and say if index part that section is zero you
do something else you do something else.
So if section is zero that is the country's so sell that tax table.
That text becomes countries every index part that's wrong.
And then I'm going to copy the same thing basic in here.
I love countries.
This will be cities.
And now we should have a table view that shows us two sections one section is for the countries one
section is for the cities.
Now before they're on the application I'm actually going to add one more method in here.
Let me delete everything because I want to add them manually to the method they want to add.
Is this title for header in Section.
So I want to see a title for header and that's going to be the return if section is zero return countries
and areas return cities and let's run the application up until this point we haven't done anything that
is new we probably never use the sections but we knew how to use them know that application is loaded
we can see that they have the title in their countries and I have I can see the names of the countries
in different cells and then I have the cities and the names of the cells in the different cities.
Now the next thing I want to do is I want to be able to swipe and delete a cell.
So to do that I'm going to go in here and say I need a new function called can edit raw at index spot
so can edit through an index that essentially allows the user to either delete or insert or move a cell
from one place to another place and you can make it dependent on the index but for instance I could
say if index part that section is zero then return true.
Otherwise don't return true.
But I'm not going to do that I'm going to say let's just return true for everyone so we can you know
edit sick cells in this entire application.
So that's the first thing they have to do to be able to edit but the next thing that we have to do which
is very important is we have to decide what should happen if you wanted to edit and it's telling me
we need an override.
Yes of course we can override why it didn't auto completed itself.
That is a curious thing.
Now the next thing is we have to say what happens when we do edit.
So that's going to be comet editing style and in the comet editing is Slide 4 index and we have to decide
what happens when you try to edit and a different editing styles.
So I'm going to just choose one of them to say if anything aside is you eye table view cell dot editing
style dot and enhancement a search for delete.
So I'm going to say if you're trying to delete if this style the user is causing is delete.
Let's go ahead and do these things.
The first thing you have to do is this you have to say whatever you're trying to delete.
We also have to remove it from our array.
Otherwise the application is going to crash simply because you've hit hard for instance six items in
your array but only five cells in your table view and the app is going to pop back an error saying the
number of items in your table view and the number of items in your array must be identical.
Because I want to change my array.
I'm going to have to change both of these to be value of us so valuable and valuable.
And in here I'm going to go ahead and say if I'm trying to delete if we ordered the delete vote then
search for if Index found that section is zero which means we are in the countryside then countries
are a dot to remove the object they want to remove is index part that's wrong.
So if for instance I'm trying to delete this one U.S. which is the item at index of one in the countries
that is index mad that's a roll one in the index might that Section Zero so a section is zero index
by the row is one I'm very moved that one from countries array which happens to be 0 and 1 it would
be the US.
So that's for the countries and then I'm going to go ahead and say else since I want to be able to delete
from both of them citizenry data remove at a game index Mark DOT rule once you remove the object from
the corresponding array being countries array or the citizenry we then have to actually delete the cell
from our table view.
So either of these two once they've done their work I'm going to have to say table view DOD delete rows
and he's gonna say which rules do you want to delete.
He's asking for an array of index parts I only have one index spot which is this one but they could
delete multiple one of those I'm going to say make an array with only this one index man on the white
table the bureau animation of Give Me Something such as let's say left so it moves to the left then
collapses and deletes so we further remove the objects from the array and then we delete the roll and
then the two of them will have the same number of objects if he had six rows and we deleted one we have
five and if we had six objects in the area with deleted one we now have only five of them.
So this time when we're on the application we should be able to swipe over some and by swiping we should
be able to see the delete.
But then we should be able to delete it accordingly delete and that one disappears.
Once again delete either Piers it happens in every different section of the application as we try to
delete when I pull here over in the RBC the delete button.
Or when we pull here over the Mexico City you still see the delete button.
What if you wanted to see which one of you are deleting or you wanted to see a different message in
here.
So for that I'm going to have to add another function here.
That function again is a built in function called title for delete confirmation.
So that is the title that appears for when you want to delete something in the table view for the index
might you are trying to delete what you want to show.
And here's what I want to show I want to see if index part that section is zero which means when the
countries return this particular text the text says this delete.
And after that delete it says this one.
What is that.
That is countries array at index path that role.
So it's saying for instance if it was over India it says delete a space India.
And then I want to have a question mark at the end of it.
It is as if I'm asking Are you sure you want to delete this one.
But I'm not going to make it as long as not because it becomes a little bit difficult to see it else
I'm going to copy this pasted in there and here it says citizenry.
So delete this one question mark and let's run the application nice again asking me for overwrite some
of the methods automatically add the overwrite some of them they don't.
So let's go ahead and run the application again and this time as we swipe and we try to delete.
We should see a clearer message asking us Are you trying to delete this one delete India.
And if it does fit is going to be within one line if it doesn't fit such as this one is actually going
up within two lines it's saying delete.
Mexico City.
Yes I want to deleted delete.
Mexico.
Yes.
I want to delete it.
So far we have managed to show two different sections and we have been able to delete some items from
them.
Now what if we wanted to move some items are essentially what happens if we wanted to be able to rearrange
our objects so to do that.
I'm going to go in here into my view this lot.
And in my view did Lord I'm going to go ahead and get rid of everything except for the line that says
navigation item.
Right.
But by then item becomes an edit by then item if you're on the application right now.
We will see the edit by then.
I'm impressed at it by then we see a little icon next to each cell but they don't really do anything.
When I press that we see that one we can press them.
That is just going to show us that delete one.
It doesn't show us anything for moving cells which is what we wanted to see.
So to do that just like what we did for deleting which was can edit row we now have to add something
that is called Can the move rule.
So I'm going to come in here at the very bottom.
I'm going to say can move roll at index that and I'm going to go ahead and say the return true.
So this function alone it essentially says the cells can move but it doesn't say how they're going to
move and how they're going to rearrange themselves.
And for that we need two other methods in fact and I'm actually going to write both of them in here.
So we get a sense of why we need to different methods in one of them.
We have to decide Veer we are going.
And then in the other function we have to go there what they mean by that is this if you're trying to
rearrange items in yellow you're pulling the Mexico and you're dragging it over your tables.
We need to know whether you can drop it within the cities or not or whether you can only drop it within
the countries.
Once that path is done when we do drop it.
Now the arrangement of the items have changed.
Now for instance if Mexico is setting up of us it means the items in the array they have a different
order.
So we have to know where we're going.
And once we go there we have to read all their objects to make sure it reflects the current shape of
our table view.
So the first one is target index part time get index part for move raw at so this is a very long you
know kind of function.
I will explain to you what it does and why is it so long.
It essentially says as you are trying to move there is the target in this spot for move from the roll.
This is a source in next man unveils the proposed one as you are trying to drop it.
Where does the application feel.
It is the index pad that you are targeting because you can't manually sell your user that I get from
index zero and go to index 5 4.
You can say is that I get from here.
Drop it somewhere on there.
And that feels to be the index part at five.
And if you add the water of the two of them.
Then again the app has to decide for you whether it is index part four or five.
And that becomes the proposed destination index spot.
And what you usually want to do in here is this you want to say just return this.
So you want to say return whatever is the proposed destination return.
Because it's asking for an index map to be written.
However there are situations that you don't want to drop it at the proposed destination index math for
instance in this application in particular I don't want my user to be able to drag from countries into
cities or vice versa.
So if you're changing the sections I don't want him to be able to do that.
To do that I'm going to go ahead and say if and I'm going to say source index part which is this guy.
That section is not the same as a proposed destination index.
That section if these two are not two within the same section do this return source just drop it back
where you found it otherwise put it at the destination so by now a user can actually move rose and they
can actually drag them around and they can find out where is the proposed destination for them whether
they're going to go back to where they come from or they're going to go to a new place.
But we haven't even told them what to do there how to go there and that is a function called move rule.
So not can move road.
So it's called Move roll at roll out.
So that is the one that they want.
So this one essentially works if you already can move.
And if you already know what is the destination and spot that comes through dysfunction.
If you do no worse off now then you can actually execute the movement and to do the execution.
Here's what we're gonna be doing.
Let me add the overwrite.
So that error shuts up if the source index path that section was zero.
If you're moving within the first section so let's do some things in there.
Else you have to do other things for the cities for the countries here's what they have to do.
I have to say let's find the element that we are trying to solve.
To do that I'm going to say let let's call it element that becomes country as array from or source index
part dot or.
So we find the object we are trying to move around.
Once we do that we're going to say countries are array dots removed and the object we're removing is
source index card that's raw.
So we get rid of that in the array.
Once that is taking care of and then go ahead and say countries agree that insert and inserting a new
element which is the old element at a new location which is the destination index part that rule.
So essentially in our array we remove one object and replace it somewhere else.
Imagine if the source index match was one destination was three.
So here 1 and 3.
Ok so here's what we are doing.
So this is one we are deleting it.
This is 0 1 2 3.
You're pasting it in there.
So you're reordering the objects but I'm not going to do that right now we're going to go back to what
we had
so here we got down on taking care of.
And of course we have to repeat exactly the same thing for the other section except date will be city's
array for everything.
So again if we're moving we find the source index cards the role in the city's array and we do everything
in the cities.
Let's run the application up until this point
so now when I pressed the Edit By then we get these little icons on the right that allow us to rearrange
objects and I can essentially bring something in here and as I'm moving them the app automatically finds
the proposed destination.
And when I drop it it places in there rearranges my array and if I try to bring something for instance
from the cities New York into the countries it doesn't along.
It goes back to its source within the right section as showed one more time if I tried to push Canada
all the way at the bottom.
It actually works but Canada into cities it doesn't work.
And once I'm done I'm going to had the Done button.
Now there's only one more thing I want to show you and that's just to do things a little bit simpler.
You don't really have to do any of these things.
This whole process of doing if it is a section do this else too.
The other thing it seems a little bit tedious.
What if we could simplify this process.
Now the good news is we absolutely could do that.
And if you're working on a smaller application you could actually have an array of arrays.
If you're having a slightly larger application you could essentially have a dictionary of arrays in
here I'm actually going to go ahead and have an array of arrays.
I'm going to say array of arrays which is of the type array of array of strings.
So this becomes a little bit of a complicated process but essentially it tells me what's going on.
It is an array of arrays of strings and why is it like that.
Because array of arrays soon enough in our view this load becomes country's array and cities array just
like that.
So that really helps me placing everything within one array and you cannot do the initialization in
here in here if you try to do that if you try to say put this part in here.
It's actually not going to work is going to give you an error saying that you cannot initialize objects
in here cannot use our instance member routine property initialization.
So you must do it within here.
So I'm going to get rid of all of these I'm going to push it back the way it used to be and now we have
an array of arrays that has countries and cities and let's see how that's going to simplify my life
in the number of sections I'm going to say return array of array that counts in the number of rows I'm
going to say get rid of everything and do this say return array of arrays find the right section and
find the right count of that because I know there is an array written here but there the section is
0 over this section is 1 when it comes to here when we are doing this cell for raw at index Mart I don't
need to go through countries and cities independently I'm gonna do this I'm going to say sell that text
label becomes array of arrays not that array of arrays then from that find index part that section so
we end up in one of the two arrays from that which is an array itself find index part got to roll.
So it essentially becomes one array which is for instance the countries from that a certain location
and other array which if the section is one becomes the cities from that.
This is a road that you're looking for.
So that really helps this part we can't really change it on this.
We had a dictionary that had keys for the both countries and the cities.
Here again will end up in a place that depending on which one we are trying to edit we remove that one.
So I'm going to say array of arrays are and I'm going to go through the index pad that section then
I'm going to say remove and remove is going to be index pad dot roll so we can delete all of these things
it simplifies our life in here as well.
Then we go in here which says which one do you want to delete.
Once again we will have return and then the return I'm going to say delete what they want to delete
is this
array of arrays and from that I want to get the index pad that section once I found that I want to get
the one that says index dot rule.
So I don't need any of these guys.
They would need is this delete find the right array from their res and find the right set of the front
then I can move wrong we don't need to do anything this part we don't need to do anything and here once
again life would be a little bit simpler by saying let them become array of arrays index part and that's
going to be the source index part source index part of that section
that section and then from there.
I want to get the source index part diet to roll.
So we find this specific elements that we are trying to remove or we're trying to move to the other
place.
Then I'm going to go ahead and say reauthorize source index smart diet section.
Now we find the right are a countries or the cities that remove.
And the one they want to remove is the one that they want to rearrange which is essentially source index
smart diet to roll once again.
So from the array of arrays removed that and then we're gonna go ahead and say array of arrays source
index math dart section dart insert and I want to insert the elements at destination.
Now some of the things I did in here it might seem that I'm actually making things more complicated
especially if you are newer at programming.
You might feel that OK.
This isn't easier by any stretch is actually more complicated but once you get a better sense of programming
and you feel a little bit more comfortable you will know that you will prefer less code.
And even if it means more complicated code such as things such as this one in saying that you could
actually always go ahead and say in any of these I'm going to convert that into a variable so I know
exactly what I'm talking about.
I didn't do that in here but that would be another option for you.
Let me run the application to make sure that everything we just did is actually functioning and nothing
has actually gone wrong which I don't really think so.
But just sort of we are short of it.
So now we can delete and we can actually edit and we can move or we cannot move within sections but
we can certainly move in a section and that's all we needed to doing here.
So these are different methods that you use for you know more advanced table view operations.
Let's keep it up to here and move on to our next lesson.