I have a UICollectionView which loads images from an api for users to rate, which works perfectly well. My problem is I want a way to show the user the selected the cell with an emoji based on their rating type (Good, bad etc). Clicking on a cell shows a modal which the user will rate, I have been able to pass data from the modal to the main collection view. Now I want to be able to use the data from the modal to display an emoji on the cell to allow the user to see what they chose
//my protocol delegate for the modal
protocol ModalDelegate {
func changeValue(userChoice: String, rateMovieID: String, rateImageUrl: String)
}
//Main class conforming to modalDelegate
class GuestRateMovieView: UIViewController, ModalDelegate {
func changeValue(userChoice: String, rateMovieID: String, rateImageUrl: String) {
self.userChoice = userChoice
print("choice =>", userChoice, "id =>", rateMovieID, "url =>", rateImageUrl)
}
}
//My custom cell
class MoviesCollectionCell: UICollectionViewCell {
private var movieImages = NSCache<NSString, NSData>()
weak var textLabel: UILabel!
let movieImage: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.clipsToBounds = true
image.contentMode = .scaleAspectFill
image.layer.cornerRadius = 10
return image
}()
let btnRate: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.clipsToBounds = true
image.contentMode = .scaleAspectFit
image.alpha = 0
return image
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(movieImage)
movieImage.addSubview(btnRate)
}
override func prepareForReuse() {
super.prepareForReuse()
movieImage.image = nil
}
func configure(with urlString: String){
//check for cached image
if let imageData = movieImages.object(forKey: urlString as NSString){
// print("using cache image")
DispatchQueue.main.async {
let image = UIImage(data: imageData as Data)
self.movieImage.image = image
}
}else{
guard let url = URL(string: urlString) else{
return
}
//Downloading images
URLSession.shared.dataTask(with: url) {[weak self] data, _, error in
guard let data = data, error == nil else {
return
}
DispatchQueue.main.async{
self?.movieImages.setObject(data as NSData, forKey: url.absoluteString as NSString)
let image = UIImage(data: data)
self?.movieImage.image = image
}
}.resume()
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
NSLayoutConstraint.activate([
movieImage.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10),
movieImage.topAnchor.constraint(equalTo: contentView.topAnchor),
movieImage.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -10),
movieImage.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
btnRate.centerXAnchor.constraint(equalTo: movieImage.centerXAnchor),
btnRate.centerYAnchor.constraint(equalTo: movieImage.centerYAnchor),
btnRate.widthAnchor.constraint(equalToConstant: 30),
btnRate.heightAnchor.constraint(equalToConstant: 30)
])
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reUseMoviesCellID, for: indexPath) as! MoviesCollectionCell
var movieTitle = [String]()
for obj in moviesArray {
if let movieObj = obj as? NSDictionary {
let packShotObj = movieObj.value(forKey: "packShot") as! NSDictionary?
let id = movieObj.value(forKey: "id") as! String?
let title = movieObj.value(forKey: "title") as! String?
if let packShots = packShotObj{
let thumbs = packShots.value(forKey: "thumbnail") as! String?
if let thumbnails = thumbs{
movieThumbnails.append(thumbnails)
movieTitle.append(title ?? "")
}
}
}
}
let imageUrl = movieThumbnails[indexPath.row]
cell.movieImage.image = nil
cell.configure(with: imageUrl)
if userChoice == "Hate it" {
cell.btnRate.image = UIImage(named: "HateIt")
moviesCollectionView.reloadData()
}
return cell
}
A short demonstration on what I want to achieve. https://www.seasfarm.com/collections.mp4