Ray Wenderlich Course Part 13 (up to lesson 121)
Another train another Ray course! I have about 20 minutes now, then another 20 minutes later. The rest I will have a crack at when I'm in a non-travelling situation! So, I am about to embark on the next Xcode project, after - to varying degrees - navigating my way through the theoretical parts of Ray's course. I am not going to waste any more time writing about those but will be cracking on now!
So I am going to make a 'checklist to do' app. It will be using table views - I used this before in Pasan's course. It's supposed to be the most important control to master. The instructor (can't remember his name!) outlines what else I am going to bed doing. His name is Brian by the way!
Something I have not been doing is the course book. If I get stuck I WILL have a look through before seeing the solution!
App Design
Brian shows the todo App. It looks pretty simple to be honest.
Table Views
Data is shown in rows. Cells are used.
Dynamic cells - prototypes. This means we can edit the cell while the app is running.
Static cell - can't be changed while app is running.
We will have a combination of both for this app.
*I've had to increase the speed to 1.25. Brian speaks SO slowly!
Right so Brian has shown how to get rid of ViewController and have the TableViewController instead. A few settings needed changing.
Table View Cells
Table view cells are recycled. But how?
*Now back to normal speed!
Reuse identifier - a string that is used. There's more to this obviously!
Adding a cell is easy.
A few more settings changed....
There are a coupe of override methods used:
So this means that the cell with the exclamation mark is now there.
*Label Challenge - done!
Protocols
Now I know from before that these are a 'blueprint for behaviour'. It doesn't matter what code you do, as long as the outcome is the same!
A standard set of methods that a class must adhere to.
It means you can add extra functionality to a class.
His explanation is not so clear. However I get the basic idea.
*Challenge - add more rows. Easy peasy!
Adding Cell Content
Tags - this is something really useful that Angela showed before.
Recycling cells - dequeue reusable cell method. If there is not a cell available, it will create a new one for you!
Index paths - use the row number as and when
Section numbers - these and rows start counting from 0
A couple of points with the above. I have put in a tag of 1000 for the label. As - forced down casting has been used. It's not too safe as if it were not a label the programme would crash!
*Challenge - add more content!
This seemed so easy that I thought there might be a catch! Wasn't one....
Table View Delegates
Sending a message that something has occurred - then letting the delegate deal with it.
Here is the code I followed with Brian:
So I am going to make a 'checklist to do' app. It will be using table views - I used this before in Pasan's course. It's supposed to be the most important control to master. The instructor (can't remember his name!) outlines what else I am going to bed doing. His name is Brian by the way!
Something I have not been doing is the course book. If I get stuck I WILL have a look through before seeing the solution!
App Design
Brian shows the todo App. It looks pretty simple to be honest.
Table Views
Data is shown in rows. Cells are used.
Dynamic cells - prototypes. This means we can edit the cell while the app is running.
Static cell - can't be changed while app is running.
We will have a combination of both for this app.
*I've had to increase the speed to 1.25. Brian speaks SO slowly!
Right so Brian has shown how to get rid of ViewController and have the TableViewController instead. A few settings needed changing.
Table View Cells
Table view cells are recycled. But how?
*Now back to normal speed!
Reuse identifier - a string that is used. There's more to this obviously!
Adding a cell is easy.
A few more settings changed....
There are a coupe of override methods used:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChecklistItem", for: indexPath)
return cell
}
*Label Challenge - done!
Protocols
Now I know from before that these are a 'blueprint for behaviour'. It doesn't matter what code you do, as long as the outcome is the same!
A standard set of methods that a class must adhere to.
It means you can add extra functionality to a class.
His explanation is not so clear. However I get the basic idea.
*Challenge - add more rows. Easy peasy!
Adding Cell Content
Tags - this is something really useful that Angela showed before.
Recycling cells - dequeue reusable cell method. If there is not a cell available, it will create a new one for you!
Index paths - use the row number as and when
Section numbers - these and rows start counting from 0
let label = cell.viewWithTag(1000) as! UILabel
A couple of points with the above. I have put in a tag of 1000 for the label. As - forced down casting has been used. It's not too safe as if it were not a label the programme would crash!
*Challenge - add more content!
if indexPath.row % 5 == 0 {
label.text = "Walk the dog"
} else if indexPath.row % 5 == 1 {
label.text = "Brush my teeth"
} else if indexPath.row % 5 == 2 {
label.text = "Do some coding"
} else if indexPath.row % 5 == 3 {
label.text = "Watch GoT"
} else if indexPath.row % 5 == 4 {
label.text = "Read a book"
} else {
label.text = "Sleep"
}
Table View Delegates
Sending a message that something has occurred - then letting the delegate deal with it.
Here is the code I followed with Brian:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
if cell.accessoryType == .none {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
tableView.deselectRow(at: indexPath, animated: true)
}
}
Right so that's it! Not the end of the app but the end of this section. There were some useful aspects and it was good to see how the TableViews work in practice. Not much else was useful at this stage, just looking over my notes in this blog. Not blown away yet but I'm going to do the second part in a bit, then crack on with the rest. After that, I think I'll be done with Ray's course, for the time being at least.
Comments
Post a Comment