【发布时间】:2020-07-26 17:09:51
【问题描述】:
我有一个网格电影的视图。共有三个属性:标题标签、海报电影的图像视图和显示电影是否受欢迎的图像。
单击单元格时,进入详细视图,显示其他有关电影的信息,并且有一个按钮操作来支持电影。我希望更新网格集合视图中的按钮图标。所以我创建了一个委托来监听这个事件发生时,然后重新加载集合视图。 截图:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! MoviesCollectionViewCell
cell.titleLabel.text = popularMovies[indexPath.row].title
getImageMovies(imageURLString: popularMovies[indexPath.row].poster, imageView: cell.movieImage)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let detailMovieViewController = DetailMovieViewController()
detailMovieViewController.titleMovieLabel.text = popularMovies[indexPath.row].title
detailMovieViewController.releaseDateMovieLabel.text = popularMovies[indexPath.row].date
detailMovieViewController.overviewMovieLabel.text = popularMovies[indexPath.row].overview
getImageMovies(imageURLString: popularMovies[indexPath.row].poster, imageView: detailMovieViewController.movieImage)
getGenresMovies(genresMoviesID: popularMovies[indexPath.row].genre, genreMovieLabel: detailMovieViewController.genreMovieLabel)
collectionView.deselectItem(at: indexPath, animated: true)
detailMovieViewController.delegate = self
selectedIndexPath = indexPath
self.navigationController?.pushViewController(detailMovieViewController, animated: true)
}
protocol favoriteMovieDelegate: class {
func updateFavoriteImage ()
}
@objc func markFavoriteButtom (buttom: UIButton){
if buttom.isSelected == false {
buttom.isSelected = true
}else {
buttom.isSelected = false
}
delegate?.updateFavoriteImage()
}
func updateFavoriteImage() {
if let indexPath = selectedIndexPath {
let cell = collectionView.cellForItem(at: indexPath) as! MoviesCollectionViewCell
cell.favoriteIconImage.image = UIImage(named: "favorite_full_icon")
collectionView.reloadData()
}
}
struct Films: Codable {
let id: Int
let title: String
let poster: String
let genre: [Int]
let date: String
let overview: String
enum CodingKeys: String, CodingKey {
case id
case title
case poster = "poster_path"
case genre = "genre_ids"
case date = "release_date"
case overview
}
}
【问题讨论】:
-
你没有在 cellForItem 中设置
favoriteIconImage? -
发布您的
cellForItem方法 -
而不是直接更新您的 cell.favoriteIconImage.image 您将不得不直接更新您的对象,然后重新加载 CollectionView。并作为@jawadAli,提到你需要在 cellForItem 上设置 fav 图片
-
@jawadAli 不,默认情况下,单元格以带有最喜欢的灰色图标的图像开头。影片的片名和图片来自API The Movie DB
-
@jawadAli。我把方法 cellForItem
标签: ios swift collections view