0
1
00:00:01,020 --> 00:00:03,640
So did you manage to complete the challenge?
1

2
00:00:03,660 --> 00:00:09,420
I really hope that you gave it a good go because it is a really, really good challenge to understand
2

3
00:00:09,510 --> 00:00:13,310
these complex and rather abstract concepts.
3

4
00:00:13,530 --> 00:00:16,110
And I imagine that if you played around with it quite a bit,
4

5
00:00:16,230 --> 00:00:21,450
fix some bugs, and figure out how to make it work inside the TodoListViewController,
5

6
00:00:21,510 --> 00:00:29,100
then you've got a good grasp of how these-- of how superclasses, subclasses, and all of this works.
6

7
00:00:29,110 --> 00:00:33,560
So spoiler alert, I'm going to show you the solution now.
7

8
00:00:33,600 --> 00:00:35,100
So if you haven't tried it,
8

9
00:00:35,100 --> 00:00:36,970
then go back and do it now.
9

10
00:00:37,230 --> 00:00:37,550
All right.
10

11
00:00:37,560 --> 00:00:42,620
So the first thing that we need do inside TodoListViewController, just as we did inside the 
11

12
00:00:42,620 --> 00:00:48,020
CategoryViewController is to change the inheritance. Instead of subclassing 
12

13
00:00:48,030 --> 00:00:50,020
UITableViewController, Apple's component,
13

14
00:00:50,040 --> 00:00:53,430
we're going to subclass SwipeTableViewController.
14

15
00:00:53,610 --> 00:01:00,290
And if you think about it, the UITableViewController is almost like the grandad of TodoListViewController
15

16
00:01:00,360 --> 00:01:07,170
now. Because inside SwipeTableViewController, it now inherits TableViewController.
16

17
00:01:07,200 --> 00:01:13,590
So, say, if this was granddaddy, then this is going to be your dad, and this is going to be you.
17

18
00:01:13,650 --> 00:01:19,330
Now, if it's a little bit upsetting to think of yourself as a block of code, then I apologize for that,
18

19
00:01:19,500 --> 00:01:21,050
but I think it's a good analogy.
19

20
00:01:21,150 --> 00:01:27,180
So the next thing that we need to do is to change our cellForRowAt an indexPath method because we're no
20

21
00:01:27,180 --> 00:01:30,040
longer going to be dequeuing our cell inside here,
21

22
00:01:30,090 --> 00:01:32,770
instead we're going to be using the superclass' cell.
22

23
00:01:32,820 --> 00:01:41,700
So we're gonna say let's cell = super, which you can see is the SwipeTableViewController,
23

24
00:01:42,240 --> 00:01:46,990
.tableView, cell for indexPath.
24

25
00:01:47,250 --> 00:01:53,770
So, now that we have this reference to the cell that is from the method inside our super view controller
25

26
00:01:53,790 --> 00:02:01,590
so inside here, and that cell is already set up as a SwipeTableViewCell and the cell's delegate is
26

27
00:02:01,680 --> 00:02:02,560
self.
27

28
00:02:02,580 --> 00:02:10,370
Now, it might be worth just going back and making sure that table view cell is set as a 
28

29
00:02:10,380 --> 00:02:10,990
SwipeTableViewCell,
29

30
00:02:11,130 --> 00:02:13,840
and it uses the SwipeCellKit module.
30

31
00:02:13,860 --> 00:02:19,520
We did that for the first cell in the tutorial and this might be something that you would have overlooked.
31

32
00:02:19,560 --> 00:02:25,310
So once that's done, then we've got this reference to the cell from our superclass.
32

33
00:02:25,620 --> 00:02:27,920
And we're going to set the cell's textLabel,
33

34
00:02:27,960 --> 00:02:34,200
we're going to set the cell's accessoryType, and then we're going to return the cell. And we don't have
34

35
00:02:34,200 --> 00:02:38,110
to deal with any of the SwipeCellKit over here.
35

36
00:02:38,130 --> 00:02:45,150
Now, the next thing to add in is that we will need to override that function that we've got inside
36

37
00:02:45,150 --> 00:02:49,550
SwipeTableViewCell which is the updateModel(at: indexPath).
37

38
00:02:49,800 --> 00:02:56,580
So we're going to go over here. We're going to override updateModel(at: indexPath) and we're going to
38

39
00:02:56,580 --> 00:03:08,100
take that indexPath to delete our data. So we can say if let item = todoItems? which is an
39

40
00:03:08,100 --> 00:03:16,610
optional, so we'll use optional chaining to grab a hold of the item at indexPath.row,
40

41
00:03:16,710 --> 00:03:24,170
and this is the indexPath that gets passed in through our superclass method updateModel an indexPath
41

42
00:03:24,450 --> 00:03:30,700
which gets triggered, if you remember, when the user swipes on a particular cell to try and delete it.
42

43
00:03:30,720 --> 00:03:38,130
So we're going to grab a reference to our item at this particular index. And if this is not nil, then
43

44
00:03:38,190 --> 00:03:46,680
we are going to use our realm.write method to make some changes inside our Realm. And the changes that we
44

45
00:03:46,680 --> 00:03:56,430
want to make are realm.delete. And the object that we want to delete is this item Object.
45

46
00:03:56,430 --> 00:04:01,210
Now, as Xcode is helpfully telling us realm.write can throw,
46

47
00:04:01,290 --> 00:04:03,780
so we have to mark it with a try,
47

48
00:04:04,020 --> 00:04:07,590
and therefore, we're going to put it inside a do-catch block.
48

49
00:04:11,800 --> 00:04:14,220
And now we're ready to run our app.
49

50
00:04:14,290 --> 00:04:20,170
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

51
00:04:20,170 --> 00:04:25,130
did before using our didSelectRowAt indexPath delegate method.
51

52
00:04:25,360 --> 00:04:32,460
But now we have the special power of swiping and getting that Delete show up,
52

53
00:04:32,530 --> 00:04:39,450
and if we click on it or swipe all the way, then it deletes it from our list of items.
53

54
00:04:39,460 --> 00:04:46,600
Now, what if we wanted our items to have the same height as the ones inside our categories?
54

55
00:04:46,600 --> 00:04:53,920
Now, we can either copy this rowHeight over to our TodoListViewController. But if we're going to have
55

56
00:04:53,920 --> 00:04:59,740
it the same for both and we're inheriting from swipe view table view controller, then it makes
56

57
00:04:59,740 --> 00:05:07,750
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

58
00:05:08,050 --> 00:05:10,150
in both view controllers.
58

59
00:05:10,510 --> 00:05:12,460
So, all right, how did that go?
59

60
00:05:12,550 --> 00:05:14,130
Did you manage to get in?
60

61
00:05:14,260 --> 00:05:20,140
If not, make sure that you have a look at the previous lesson as well as this one, and try and go and
61

62
00:05:20,140 --> 00:05:21,340
attempt it again.
62

63
00:05:21,370 --> 00:05:28,090
It's really, really important that at this stage, that you really try things yourself. And, you know, you
63

64
00:05:28,090 --> 00:05:32,390
will fail, and you will fall over, and you will have to struggle a lot,
64

65
00:05:32,470 --> 00:05:36,300
but believe me, the more that you struggle, the more that you learn.
65

66
00:05:36,310 --> 00:05:38,290
So that's all for this lesson.
66

67
00:05:38,290 --> 00:05:45,940
And in the next lesson, we're going to improve the appearance of our app even further by adding gradients
67

68
00:05:46,000 --> 00:05:46,840
to our cells.
68

69
00:05:46,930 --> 00:05:49,930
So for all of that and more, I'll see you on the next lesson.
