All right. So, now that we've added the basic functionality to our app which is simply displaying some 1
text from array into a table view, the next step is to set it up so that we can start making some in-app 2
purchases. 3
Now in order to do that, I'm going to add a cell at the very bottom of all of our quotes that says, 4
"Buy more quotes." 5
And when I click on that cell, it's going to trigger the purchasing process. 6
So first things first, 7
currently, we're only loading up six cells because we're looking at quotes to show and we're looking 8
at the count which is six. 9
Now, if we want to add that extra cell, then all we have to do is just add one. And now we'll always have 10
one extra cell on top of all the cells we need to show the quotes. 11
So, now that we have seven cells, but we only have data for six of them, 12
so what we can do here is we can add an "if" statement that checks if the indexPath.row of the cell 13
that we're trying to render the content form is less than quotesToShow.count, then we're going 14
to populate it with text from the array that's quotesToShow. 15
But if it's greater than that number, namely, if we've hit that last cell where indexPath is equal to 16
6,j 17
so remember we start counting from 0, 18
so this one has an indexPath.row of 0, 1, 2, 3, 4, 5, 6, and that one is going to have a special cell text. 19
So cell.textLabel.text is going to equal "Get More Quotes." 20
And this is going to be our main Buy button. 21
So in order to show that it's different from the rest of the cells, we're going to change its text color. 22
So cell.textLabel.textColor = blue color, and we're going to use a Color Literal here. 23
So if you start typing color and you can see you've got Color Literal being suggested, 24
go ahead and double click on it, and you will get a little square. 25
And now, if you double click on the square, we get a color picker including recently used colors. 26
So I'm going to stick to the color that I've got in the theme of this app which is that kind of green 27
bluey turquoise color, not very good at describing colors, but that one. 28
And you can, of course, choose whatever it is that you wish, 29
but make sure that it's visible. 30
And then we're going to change the cell's accessoryType to something called a disclosureIndicator. 31
And this has a control shaped like a chevron, 32
so like a little right-angle bracket, and this control indicates the user that tapping the cell triggers 33
an action. 34
So that means that we can use this to indicate to the user that you can actually press on the cell and 35
something will happen. 36
So let's go ahead and run our app and see what that looks like so far. 37
So we currently have six quotes and the very last cell is this button that says, "Get More quotes," with 38
our little disclosureIndicator and our text color being the same as this one which looks pretty neat, 39
I think. 40
All right. So, now that we're done with that, the next step is to detect a touch on that cell. 41
So I'm going to go ahead and delete all of the rest of this comment and template code that Apple gave 42
us because it's not very useful, 43
and we're going to write it ourself. 44
So currently, we've got a pragma MARK up here that's the Table view data source method. 45
And I'm just going to copy it and create another one down here which are going to be the Table view 46
delegate methods. And one of the main delegate methods that we use all the time is, of course, 47
tableView didSelectRowAt indexPath. And you can see that you've got two methods being suggested and they 48
look pretty much identical on here. 49
Now, they are actually not identical because if I hit enter on the top one, you can see that I get a normal 50
function being autosuggested here. 51
And I'll also get an error saying that I need to put the 'override' keyword. 52
Now, whereas if I chose the one that is the second one in that list, then I get that override keyword for 53
free. 54
Now, if you remember the reason why we need that override keyword is because we're inheriting from 55
UITableViewController, 56
and that class already has the method tableView, didSelectRowAt indexPath and we're overriding its 57
functionality to provide our own functionality. 58
So this is the line of code that we need. 59
So inside here, we're going to check to see if the row that was selected has a indexPath.row 60
of the last item in this table view. 61
So that is, again, going to be number 6 because 0, 1, 2, 3, 4, 5, 6, and that is going to be equal to the number 62
of quotes to show. 63
So if indexPath.row is equal to quotesToShow.counts, then, in that case, we're pretty certain 64
that this button got pressed, and then we're going to do something special. Before we do anything special, 65
let's just check to make sure it works. 66
Let's write print "Buy quotes clicked" and let's hit Run. 67
And now if I click on this one, you can see "Buy quotes clicked." 68
Perfect. 69
So it's now working. 70
If this line of code is at all confusing to you and why we're using quotes ToShow.count, then 71
make sure that you haven't skipped any of the lectures before and you've completed all of the previous 72
modules before you got to this current module. 73
I know that it's really tempting to just go straight for the one that you really need right now. 74
But without understanding everything, 75
it's very difficult to implement this into your own app. 76
So I recommend going through all of the modules in the order that they are in the curriculum. 77
It's really important. 78
I can't stress enough that you don't skip the modules. 79
Okay. 80
Either way, we've now determined that the last button got clicked and we can delete this print statement. 81
And now this is the place where we are going to implement the buying of our in-app purchase. 82
So I'm going to call a method called buyPremiumQuotes and this method doesn't yet exist, 83
so let's go ahead and create it down him. 84
Now, in order to create it, I'm going to create a new section. So I'm going to create a new pragma MARK 85
and this one is going to be called the in-app purchase methods, and here I'm going to create that function 86
called buyPremiumQuotes. 87
And let's just make sure that we spelled it right by checking to see that this actually changes color 88
and we don't get any errors by hitting command B to just force a build and compile all of our code to 89
make sure that everything is working. Okay. 90
So, now when the user clicks on this button, then this part triggers and this function will get called. 91
Now, there's just one slight problem here and that's when you press on this button, the cell does not 92
get deselected automatically. And it looks a bit weird because, normally, when you click on a cell, it 93
should turn gray, and then quickly turn back to white. 94
That looks a lot better than having it permanently selected. 95
And we can, of course, get rid of that by simply adding a line of code that says 96
tableView.deselectRow at indexPath, and that's, of course, the current indexPath, 97
animated.: true. 98
And now when we select that cell, it will de-select itself automatically with a quick animation and that 99
just makes it so much nicer in terms of the user experience. 100
Okay. So, in this lesson, we have set up our cell that has the text of "Get More Quotes." 101
We change its color and disclosureIndicator to make it look like it's clickable. And we've also got 102
a delegate method that gets triggered when it gets pressed, and we're using it to trigger a function 103
called buyPremiumQuotes so that we can start processing the in-app purchases. 104
Now, in the next lesson, we're going to be looking at StoreKit and the StoreKit API in order to query 105
the Apple databases and implement the code to buy our premium quotes in-app purchase. 106
So for all of that and more, I will see you on the next lesson.