Ray Wenderlich Course Part 14 (up to lesson 142)
So, continuing! I'm going to try to get through as much of the next three sections as possible. The next bit is about MVC. I know about that already. So here we go!
So Brian mentions that we'll code the project in a sub-optimal way, then do it with MVC. I think I know what this will mean - that the code will repeat itself and not the use the 'model' part of it.
Model View Controller
This is a template solution to help you solve problems. I've used with MVC and actually already created a folder for each of these, then got the files that match up to these.
It is a paradigm basically. There is communication between these that means you don't have to re-code/repeat yourself.
MVC - First Attempt
Refactoring - a posh way of saying rewriting/redoing.
What a waste of time! It was very difficult to get all of this code in and it doesn't even seem to work after all of that!
Well it was fine after all - just one error in the number of rows but still very verbose and unnecessary.
There are various other changes made, but I don't see the point of copying it all here. Just going to play it out.
Incorporating Arrays
OK, I'm going to look ahead and create an array that is part of the class...
OK completely wrong about that! Let's see how Brian does it....
Right so the code has been refactored - simplified and duplications removed. I get that but what a roundabout way!
Still, useful to see the difference between verbose/unnecessary code and succinct, efficient code! I don't get a couple of people demanding what a waste of ten minutes etc. Worth persevering with, and will hopefully make me think along more organised lines!
Adding a New Screen
So a new ViewController (table view controller) has been added with new buttons, code...
So Brian mentions that we'll code the project in a sub-optimal way, then do it with MVC. I think I know what this will mean - that the code will repeat itself and not the use the 'model' part of it.
Model View Controller
This is a template solution to help you solve problems. I've used with MVC and actually already created a folder for each of these, then got the files that match up to these.
It is a paradigm basically. There is communication between these that means you don't have to re-code/repeat yourself.
MVC - First Attempt
Refactoring - a posh way of saying rewriting/redoing.
let row1Text = "Walk the dog"
let row2Text = "Brush my teeth"
let row3Text = "Do some coding"
let row4Text = "Watch GoT"
let row5Text = "Read a Book"
if indexPath.row % 5 == 0 {
label.text = row1Text
} else if indexPath.row % 5 == 1 {
label.text = row2Text
} else if indexPath.row % 5 == 2 {
label.text = row3Text
} else if indexPath.row % 5 == 3 {
label.text = row4Text
} else if indexPath.row % 5 == 4 {
label.text = row5Text
} else {
label.text = "Sleep"
}
Well it was fine after all - just one error in the number of rows but still very verbose and unnecessary.
There are various other changes made, but I don't see the point of copying it all here. Just going to play it out.
Incorporating Arrays
OK, I'm going to look ahead and create an array that is part of the class...
OK completely wrong about that! Let's see how Brian does it....
Right so the code has been refactored - simplified and duplications removed. I get that but what a roundabout way!
Still, useful to see the difference between verbose/unnecessary code and succinct, efficient code! I don't get a couple of people demanding what a waste of ten minutes etc. Worth persevering with, and will hopefully make me think along more organised lines!
class ChecklistItem {
var text = ""
var checked = false
}
var items = [ChecklistItem]()
required init?(coder aDecoder: NSCoder) {
let row0Item = ChecklistItem()
row0Item.text = "Walk the dog"
row0Item.checked = false
items.append(row0Item)
let row1Item = ChecklistItem()
row1Item.text = "Brush teeth"
row1Item.checked = false
items.append(row1Item)
let row2Item = ChecklistItem()
row2Item.text = "Coding practice"
row2Item.checked = false
items.append(row2Item)
let row3Item = ChecklistItem()
row3Item.text = "Watch GoT"
row3Item.checked = false
items.append(row3Item)
let row4Item = ChecklistItem()
row4Item.text = "Read a book"
row4Item.checked = false
items.append(row4Item)
let row5Item = ChecklistItem()
row5Item.text = "Cook an omelette"
row5Item.checked = false
items.append(row5Item)
let row6Item = ChecklistItem()
row6Item.text = "Sing a Radiohead song"
row6Item.checked = false
items.append(row6Item)
let row7Item = ChecklistItem()
row7Item.text = "Catch up with friends"
row7Item.checked = false
items.append(row7Item)
super.init(coder: aDecoder)
}
That's just some of the code.
Refactoring
So a few more changes - functions rather than repeated code. Cleaner code - easier to extend and maintain.
*That's the end of this section but I'm going to crack on - just want to get this app done now!
Navigation Controllers
*Challenge complete - put in an 'add item' as a bar button, then changed the style to a +, then connected it with a print message. No problem.
Several other challenges - added a random number to insert new item messages. No problems.
Swipe to Delete - here was the code:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
items.remove(at: indexPath.row)
//let indexPaths = [indexPath]
//tableView.deleteRows(at: indexPaths, with: .fade)
tableView.reloadData()
}
So a new ViewController (table view controller) has been added with new buttons, code...
class AddItemTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.largeTitleDisplayMode = .never
}
@IBAction func cancel(_ sender: UIBarButtonItem) {
navigationController?.popViewController(animated: true)
}
@IBAction func done(_ sender: Any) {
navigationController?.popViewController(animated: true)
}
}
*Challenge of adding text field and connecting it to the view controller (the new VC) - no problem.
Textfields and Responder Chain
OK so not much of what he is talking about makes sense.
I have updated the code with various methods - all hard to keep up with but I have done it.
Well that is a lot. I've done quite a few things, even if it hasn't all been clear. Anyway, here's what I've picked up:
- Adding bar button items and editing the symbol/options
- Refactoring - why it is useful and important
- Using the delete button and other buttons with bar views
That may not seem like a lot - it's not really! It's convinced me that after the next section - the rest of the current app - I'm going back to Angela's course, just to see how accessible it is at that level. If it seems too advanced or confusing (I'm sure it won't, Angela!), then I will have a look at Treehouse. More tomorrow!
Comments
Post a Comment