So did you manage to complete the challenge? 1
I really hope that you gave it a good go because it is a really, really good challenge to understand 2
these complex and rather abstract concepts. 3
And I imagine that if you played around with it quite a bit, 4
fix some bugs, and figure out how to make it work inside the TodoListViewController, 5
then you've got a good grasp of how these-- of how superclasses, subclasses, and all of this works. 6
So spoiler alert, I'm going to show you the solution now. 7
So if you haven't tried it, 8
then go back and do it now. 9
All right. 10
So the first thing that we need do inside TodoListViewController, just as we did inside the 11
CategoryViewController is to change the inheritance. Instead of subclassing 12
UITableViewController, Apple's component, 13
we're going to subclass SwipeTableViewController. 14
And if you think about it, the UITableViewController is almost like the grandad of TodoListViewController 15
now. Because inside SwipeTableViewController, it now inherits TableViewController. 16
So, say, if this was granddaddy, then this is going to be your dad, and this is going to be you. 17
Now, if it's a little bit upsetting to think of yourself as a block of code, then I apologize for that, 18
but I think it's a good analogy. 19
So the next thing that we need to do is to change our cellForRowAt an indexPath method because we're no 20
longer going to be dequeuing our cell inside here, 21
instead we're going to be using the superclass' cell. 22
So we're gonna say let's cell = super, which you can see is the SwipeTableViewController, 23
.tableView, cell for indexPath. 24
So, now that we have this reference to the cell that is from the method inside our super view controller 25
so inside here, and that cell is already set up as a SwipeTableViewCell and the cell's delegate is 26
self. 27
Now, it might be worth just going back and making sure that table view cell is set as a 28
SwipeTableViewCell, 29
and it uses the SwipeCellKit module. 30
We did that for the first cell in the tutorial and this might be something that you would have overlooked. 31
So once that's done, then we've got this reference to the cell from our superclass. 32
And we're going to set the cell's textLabel, 33
we're going to set the cell's accessoryType, and then we're going to return the cell. And we don't have 34
to deal with any of the SwipeCellKit over here. 35
Now, the next thing to add in is that we will need to override that function that we've got inside 36
SwipeTableViewCell which is the updateModel(at: indexPath). 37
So we're going to go over here. We're going to override updateModel(at: indexPath) and we're going to 38
take that indexPath to delete our data. So we can say if let item = todoItems? which is an 39
optional, so we'll use optional chaining to grab a hold of the item at indexPath.row, 40
and this is the indexPath that gets passed in through our superclass method updateModel an indexPath 41
which gets triggered, if you remember, when the user swipes on a particular cell to try and delete it. 42
So we're going to grab a reference to our item at this particular index. And if this is not nil, then 43
we are going to use our realm.write method to make some changes inside our Realm. And the changes that we 44
want to make are realm.delete. And the object that we want to delete is this item Object. 45
Now, as Xcode is helpfully telling us realm.write can throw, 46
so we have to mark it with a try, 47
and therefore, we're going to put it inside a do-catch block. 48
And now we're ready to run our app. 49
So if we go inside home, you can see that we've got three items, and we can still check them off as we 50
did before using our didSelectRowAt indexPath delegate method. 51
But now we have the special power of swiping and getting that Delete show up, 52
and if we click on it or swipe all the way, then it deletes it from our list of items. 53
Now, what if we wanted our items to have the same height as the ones inside our categories? 54
Now, we can either copy this rowHeight over to our TodoListViewController. But if we're going to have 55
it the same for both and we're inheriting from swipe view table view controller, then it makes 56
more sense for it to go over here. And now, if we hit run, you'll see that we get that nice expanded height 57
in both view controllers. 58
So, all right, how did that go? 59
Did you manage to get in? 60
If not, make sure that you have a look at the previous lesson as well as this one, and try and go and 61
attempt it again. 62
It's really, really important that at this stage, that you really try things yourself. And, you know, you 63
will fail, and you will fall over, and you will have to struggle a lot, 64
but believe me, the more that you struggle, the more that you learn. 65
So that's all for this lesson. 66
And in the next lesson, we're going to improve the appearance of our app even further by adding gradients 67
to our cells. 68
So for all of that and more, I'll see you on the next lesson.