Welcome back again.
In this lesson we will learn how to delete a cell or a series of cells in a collection view.
So let's head into court and in here I'm going to start a new application of the tape single view.
I'm going to call it deleting deleting cells in collection views.
So this is my deleting cells in collection of views.
And once my application is loaded the first thing I'm going to do is I'm going to make a new final command
in this file is going to be called Touch class UI collection view UI collection view controller.
I'm going to call it my collection view controller.
Then I'm going to delete the order view controller and I'm going to head into the storyboard and say
I don't want this outlet.
Rather I need a collection view controller which is also connected to my collection view controller
and is also my initiated view controller.
And finally I'm going to say my cell actually has an I.D. such as cell I.D. And that's all I have to
do to set up the basics of a collection view and then I'm going to head into my collection view controller
get rid of all of doors.
I don't need that and I'm going to go ahead and say let's actually get rid of everything this rewrite
everything on our own.
The very first thing I want to do is I want to say I have a certain number of items.
So let's say let item count to be 10.
This is obviously not the way you do it in a real life application.
I'm just doing it for the sake of testing essentially the next thing I need to do is number of items
in sections for the number of items in sections I'm going to say return item count and then I have sell
for I then add in next month.
And this one I'm going to go ahead and say it has a list let's call it cell that's going to become collection
viewed the Q reusing an idea which was sell a I.D. And then in the expired which is this index back
Now let's say for now my cells are only going to show two different colors something that we have done
before as of at the source cell that like on color becomes you a part of the red and if index path died
to roll modulus 2 was 0 it means if these were even cells let's go ahead and say cell the background
color becomes actually your eye color blue.
And of course at the end we have to return are said now.
Up until this very point we should be able to run our application and we should see a collection view
which doesn't have a flaw or a proper layout but it still shows me a number of cells that some of them
are red.
Some of them are blue and once they have that I can actually go ahead and add some sort of a delete
by then to each of these cells because I'm not that really bother with the flow of this maybe it makes
sense that we go through the a storyboard and we increase the size of the cells so then we run the application
we actually end up seeing them slightly larger so it is easier to select them or view them so something
like that.
Now the next thing I want to do is I want to add a delete by then to each cell in here and let me reformat
this or we get a little bit of a better viewing.
So something like that and yet I'm going to go ahead and say I need a delete button for my grid one
that I'm going to say let delete button and I'm going to make that to be a UI button.
Are you able then can be made from CGI correct.
And that's what I'm going to be doing when I see the CGI correct for this by then which is in every
single cell is somehow on the top left corner.
So it is 0 0.
Maybe the veto if it is 25 25.
So it's a delete by then that is on the top left corner of each cell.
Now the next thing I'm going to do is when I say it is by then dot background color maybe because some
of them are red.
Some of them are blue.
We make the delete by then.
Back on here you a color diet yellow.
So we can see it clearly and visibly.
The next thing I'm going to do is I'm going to go ahead and say I need a title for it.
So I'm going to go ahead and say delete by then that set title and the title of it.
It says X for delete for a state that is normal of course.
So we got that by taking care of.
Now if you remember from our topics when you were dealing with core animation with the CIA early years
on the core graphics I could go ahead and say delete my then dot to layer that corner radius.
Let's give it a little bit afraid.
Yes I may give it the radius that is half of this value so that's twelve point five.
And that essentially makes these pardons to have perfectly round corners like circular by thence.
So once they have that part taking care of then the last thing I'm going to do is I want to make sure
that this button is actually added to my cell.
So I'm gonna go ahead and say cell.
Add the subbed view and the view I want to add is that the need to then.
Now the only thing that is still missing is what happens when we press or when we tap on this by then.
And to do that I'm going to go ahead and say Dale by then start at target for the target of it.
I'm going to add a target which is here and a selector which says go for.
Delete cell.
This is an action that we still have to add in here and the event is touch up inside.
Just like what we usually do.
We now have to write the function for delete set.
So I'm going to come in here and say I have a function that function is called delete cell and I don't
have any kind of return for it.
And since I want to use that for a selector I have to make sure that's exposed as an objective c and
this is a specific to a swift for as you record.
Now before we can actually use the delete cell function there's still something that we have to change
and that is when we call the delete.
By then we go to this function but we don't know which by then from which cell is sending us here and
to fixed on and will go ahead and say My delete then actually has an argument let's call it sender which
is of the type UI by then and in the selector that argument actually gets called by calling the underscore
and then a column that says I am calling this function and this by then is essentially calling it and
now within the delete cell we actually have access to this it.
Now access to this by then alone doesn't mean we know which cell it is but we could actually get it
from it because we know the buttons are a sub view of the cell.
So let's keep that in mind.
The buttons are a sub view of the cell that's going here I say I'm going to have to find this cell so
when I say led to the cell to be sent there whatever it is that sender then super review super review
and I'm gonna get that as a you eye collection view said because I need to make sure that they get this
object send from there I get a super view from there.
I get this cell which is of the type you a collection view cell if you want to be even more careful
about this you could essentially do this you could God this thing you could see God the letters cell
and then depending on the situation you would say I have an LS in the LS I would just say return and
in the not else in the remainder of the court I would decide the right what I want to do in this case
I know cell is of the type you are a collection of your cells so I don't need to guard it but it kind
of is in generally good practice to get used to to guard in Swift.
So I'm going to keep the god I'm going to save once for each year which means we haven't returned so
I'm going to go ahead I'll say let index path to be self DOD connection view and then index path for
cell and I have to force all wrapped up one making sure that since we know the cell existing next fight
does exist as well.
And then finally I'm going to go ahead and say self DOD collection view DOD delete items at this array
of index funds under one is this one index but now we know that if we do this right now you're going
to have an error and that error is essentially the number of IDM is hasn't changed.
So because of that I'm going to go ahead and see changed item count to a variable so it can actually
change and in here when I delete one right before that I'm going to say let's add the one from or subtract
one from item count in a real life situation a real application this would be an array items that you
delete one of the items from the array and then you also delete the item from the collection view.
So that's all I have to do in here I have to find the cell I have to delete it from the items of the
collection of you and I have to respectively subtract the item from my item count or the array that's
on the application and if it all works we should be able to see a little X on the top left corner of
all of the cells any of them that the top one should get deleted and I kind of can't see heat but they
chose a bad color for it.
I apologize for that.
Maybe yellow wasn't the really good idea.
Maybe purple would be better.
And if we run this one we should be able to see them more vividly.
And then if you click on them we should be able to see them getting deleted.
So here.
When I tap on that it gets deleted.
On the other one and the other one so on and so forth.
And that is how you delete a cell in a collection of you.
This is one of the many ways there are other ways such as when you customize you a collection of view
cell and you take care of the deletion victim.
This cell cell class actually you know encouraged you guys to try different ways depending on what you
are trying to do as well.
So let's keep it up to here and move on to our next lesson.