【问题标题】:Update a single view with uicollection view Swift使用 uicollectionview Swift 更新单个视图
【发布时间】:2020-07-26 17:09:51
【问题描述】:

我有一个网格电影的视图。共有三个属性:标题标签、海报电影的图像视图和显示电影是否受欢迎的图像。

单击单元格时,进入详细视图,显示其他有关电影的信息,并且有一个按钮操作来支持电影。我希望更新网格集合视图中的按钮图标。所以我创建了一个委托来监听这个事件发生时,然后重新加载集合视图。 截图:

grid movies screen

detail movie screen

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


【解决方案1】:

您必须直接更新您的对象,然后重新加载CollectionView,而不是直接更新您的cell.favoriteIconImage.image。您需要在cellForItem 上设置最喜欢的图像,并且一旦用户将其设置为最喜欢的图像,您还需要将其发布到服务器......如果您想保留数据,则从服务器获取isFav。如果您的服务器不支持isFav,那么您需要将其存储在本地针对film Id ...在user default

【讨论】:

    猜你喜欢
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多