如何在UICollectionView单元格被点击后,根据用户评级显示对应表情图标?
UICollectionView单元格根据用户评级显示对应表情图标
我有一个UICollectionView,它从API加载图片供用户评级,功能运行正常。现在需要实现:根据用户的评级类型(好评、差评等),在用户选中的单元格上显示对应的表情图标。点击单元格会弹出模态窗口让用户完成评级,我已经实现了从模态窗口向主集合视图传递数据,现在需要利用这些数据在对应单元格显示表情,方便用户查看自己的选择。
// 模态窗口的协议代理 protocol ModalDelegate { func changeValue(userChoice: String, rateMovieID: String, rateImageUrl: String) } // 遵循ModalDelegate的主视图控制器类 class GuestRateMovieView: UIViewController, ModalDelegate { func changeValue(userChoice: String, rateMovieID: String, rateImageUrl: String) { self.userChoice = userChoice print("choice =>", userChoice, "id =>", rateMovieID, "url =>", rateImageUrl) } } // 自定义CollectionViewCell 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){ // 检查缓存图片 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 } // 下载图片 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 }
效果说明
- 最终效果:用户完成评级后,对应单元格上显示匹配该评级的表情图标
- 目标效果:支持多个单元格分别进行评级,每个单元格独立显示对应选择的表情图标
内容的提问来源于stack exchange,提问作者chevi99
相关产品推荐
相关产品推荐

