So I needed a a star rating feature to appear in my app - it doesn't need to be interactive.
I have my app connected to a database I've set up in Parse, and I've made a column for OurRatingof type Number, and I just set a number between 0.5 and 5.
In my custom cell, I have 5 images which will display my star images. The code I've done is as follows:
//I define my images at the top
var fullStarImage: UIImage = UIImage(named: "starFull.png")!
var halfStarImage: UIImage = UIImage(named: "starHalf.png")!
var emptyStar: UIImage = UIImage(named: "starEmpty.png")!
Then further down in my table method, I have:
//Star Rating System
if let ourRating = object?["OurRating"] as? Double {
if ourRating == 0.5 {
cell?.star1.image = halfStarImage
}
if ourRating == 1 {
cell?.star1.image = fullStarImage
}
if ourRating == 1.5 {
cell?.star1.image = fullStarImage
cell?.star2.image = halfStarImage
}
if ourRating == 2 {
cell?.star1.image = fullStarImage
cell?.star2.image = fullStarImage
}
if ourRating == 2.5 {
cell?.star1.image = fullStarImage
cell?.star2.image = fullStarImage
cell?.star3.image = halfStarImage
}
if ourRating == 3 {
cell?.star1.image = fullStarImage
cell?.star2.image = fullStarImage
cell?.star3.image = fullStarImage
}
if ourRating == 3.5 {
cell?.star1.image = fullStarImage
cell?.star2.image = fullStarImage
cell?.star3.image = fullStarImage
cell?.star4.image = halfStarImage
}
if ourRating == 4 {
cell?.star1.image = fullStarImage
cell?.star2.image = fullStarImage
cell?.star3.image = fullStarImage
cell?.star4.image = fullStarImage
}
if ourRating == 4.5 {
cell?.star1.image = fullStarImage
cell?.star2.image = fullStarImage
cell?.star3.image = fullStarImage
cell?.star4.image = fullStarImage
cell?.star5.image = halfStarImage
}
if ourRating == 5 {
cell?.star1.image = fullStarImage
cell?.star2.image = fullStarImage
cell?.star3.image = fullStarImage
cell?.star4.image = fullStarImage
cell?.star5.image = fullStarImage
}
}
This code WORKS! Nothing wrong with what it produces, I'm using wondering if there was a better way I could have written this? Maybe to reduce the amount of code?
Any advice is appreciated.