Cosmos
Cosmos copied to clipboard
How to modify each cosmosView rating independently in a custom Cell, and afterward save the changes.
Hi @evgenyneu,
First, I want to say thanks so much for such a great star rating control. I'm trying to figure it now for days and maybe you know the answer (at the end you the author of the repository). :)
My question is how to modify each cosmosView
rating independently in a custom UICollectionViewCell
, and afterward save the changes.
I know how saving works using UserDefaults
.
But I don't know where to call the functions (load & save) after I modify the rating on the cells, so each cell rating is modified independently from the other one.
I did tried it in multiple UICollectionView
methods like didSelectItemAt indexPath
, and cellForItemAt indexPath
This is my Custom UICollectionViewCell
of course some unnecessary code I left out.
class ProductCell: UICollectionViewCell {
//MARK: - Properties
fileprivate let kDefaults = "key"
lazy var ratingStar: CosmosView = {
let cosmosStar = CosmosView()
cosmosStar.settings.filledImage = UIImage(named: "starFilled")?.withRenderingMode(.alwaysOriginal)
cosmosStar.settings.emptyImage = UIImage(named: "starEmpty")?.withRenderingMode(.alwaysOriginal)
cosmosStar.rating = 0
cosmosStar.settings.fillMode = .half
cosmosStar.settings.updateOnTouch = true
return cosmosStar
}()
override init(frame: CGRect) {
super.init(frame: frame)
saveStarRating()
loadStarRating()
}
// MARK: - Methods
func saveStarRating() {
ratingStar.didFinishTouchingCosmos = { rating in
UserDefaults.standard.set(rating, forKey: self.kDefaults)
}
}
func loadStarRating() {
guard let loadValue = UserDefaults.standard.object(forKey: kDefaults) as? Double else {return}
ratingStar.rating = loadValue
}
}
Also, This is how the UICollectionViewCell
s look in the simulator:
Thanks in advance. :)
Hi @IlijaMihajlovic, I don't have time to help you right now, but have a look at the demo app that comes with this repository. It has a table view screen with multiple rating controls. You can see how the ratings for each control are stores in local variable ratingStorage
:
https://github.com/evgenyneu/Cosmos/blob/master/Demo/PerformanceTableViewController.swift
For your case, instead of using local variable, you can store your ratings in a local database, like UserDefaults (for small data) or CoreData.
Hi @evgenyneu,
Thanks so much for your message. I understand how saving and loading works.
But I didn't understand how to save and load the ratings on the cells, so each cell
rating is modified independently from the other one on the cosmosView
using UserDefaults
and a CollectionView
programmatically.
If you can show me the sample code for that in your spare time when you not busy, that would mean a lot to me. I'm trying to figure it out now for days without any luck. Thanks
@IlijaMihajlovic, I'm really sorry I don't have any spare time right now. I'm a student and the workload is insane currenly, I barely have time for sleeping lol. I would suggest trying your luck on stackoverflow.
@evgenyneu, Ok cool just keep the issue open, maybe somebody else will know the answer. Thanks!