RW.com: Storyboards Segues Article - Consolidation
So, as mentioned, I need to consolidate this particular article. There was so much code there, I just wanted to look over it one more time and make sense of it all - from the Xcode project.
Start Time - 13:05
Let's look at one Swift File at a time. First of all the model - this is what was already created before the article:
Start Time - 13:05
Let's look at one Swift File at a time. First of all the model - this is what was already created before the article:
struct Player {
var name: String?
var game: String?
var rating: Int
}
A simple struct here. Name and game are optional because there may be no value (nil) put in - stops the program from crashing. Rating is an int - a score out of 5. That's interesting as anything could actually be set for it! The rating part wasn't actually covered in this project.
Now we have the view file:
import UIKit
class PlayerCell: UITableViewCell {
@IBOutlet weak var gameLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var ratingImageView: UIImageView!
// 1:
var player: Player? {
didSet {
guard let player = player else { return }
gameLabel.text = player.game
nameLabel.text = player.name
ratingImageView.image = image(forRating: player.rating)
}
}
// 2:
private func image(forRating rating: Int) -> UIImage? {
let imageName = "\(rating)Stars"
return UIImage(named: imageName)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Breaking this down....
There are the three label (IB Outlets).
Then at //1
Then we have the code for creating a new player (the var player: Player?). In this there there is a guard statement - a protective way of making sure it is of type player (the custom struct from earlier) and if so, the labels will update accordingly. However, again not the rating image!
*I've put a message on the forum about this to see if I just missed something!
At //2
This seems to be a function for getting the star rating image...so why not showing up for new players!
The rest is the override functions - there is awake from nib...A quick search tells me that this is in place of viewDidLoad. I knew I'd heard of nibs before!
Then there is a setSelected one - that is about configuring the view apparently.... Not sure what that means!
I've looked at the ViewController files but there is so much complex code in here.
I've decided that it's better to leave this one for now. I'd rather get to grips with Something SwiftUI related - really understand each stage better so I can create something from scratch based on that.
Finish Time 13:25 (20 minutes - short entry!)
Now I will pick out an article that I think will be useful and I can learn some key skills from, like I did here. I can't be expected to learn EVERY element of something when there is a lot more complex code at work. From this article, it has been experience with stroryboards and segues - that was certainly worth it!
Comments
Post a Comment