繁体   English   中英

在 TableView 上滚动时,tableViewCell 中的按钮图像正在更改

[英]Button image in tableViewCell is changing while scrolling on TableView

在 tableView 中滚动时出现问题。 我在项目中实现了书签,一切正常,除了滚动时。 当它第一次运行时,它会调用 API 来检查该项目是否已添加书签。 如果加了书签,则显示它显示填充的图像,如果没有,则显示未填充的图像。 第一次加载时它工作正常,但是当我滚动书签按钮图像时会发生变化。

我尝试在 CustomTableViewCell 上使用 prepareForReuse 来解决这个问题,并且只在 cellforRow 上渲染按钮图像,但没有奏效。

这是主视图控制器:


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let itemCell = tableView.dequeueReusableCell(withIdentifier: "NewPodcastTableViewCell", for: indexPath) as! NewPodcastTableViewCell

            let allMaterial = materials[indexPath.row]
            itemCell.setMaterials(materials: allMaterial)
            itemCell.delegate = self
        return itemCell
}

extension BrowseViewController: ViewBookmarkedDelegate  {

    func setBookmark( refId: String, isBookmarking: Bool, refType: String, refSubType: String) {
            let query = SetBookmarkMutation(refId: refId, refType: "material", isBookmarking: true, refSubType: refSubType)
            Apollo.shared.client.perform(mutation: query ) { results, error in
                if var bookmarked = results?.data?.bookmark {
                   print(refId,bookmarked,"onUnbookmark")            

                } else if let error = error {
                    print("Error loading data \(error)")
                }
           }
    }

    func setUnbookmark(refId: String, isBookmarking: Bool, refType: String, refSubType: String) {
            let query = SetBookmarkMutation(refId: refId, refType: "material", isBookmarking: false, refSubType: refSubType)
            Apollo.shared.client.perform(mutation: query ) { results, error in
                if var bookmarked = results?.data?.bookmark {
                    print(refId,bookmarked,"onUnbookmark")

                } else if let error = error {
                    print("Error loading data \(error)")
                }
           }
    }
}

这是 CustomTableViewCell

protocol ViewBookmarkedDelegate {
    func setBookmark( refId: String, isBookmarking: Bool,  refType: String, refSubType: String)
    func setUnbookmark( refId: String, isBookmarking: Bool, refType: String, refSubType: String)
}

 var delegate: ViewBookmarkedDelegate?

 @IBOutlet weak var bookmarkButton: UIButton!

 @IBAction func bookmarkButtonTapped(_ sender: UIButton) {


    if (self.materials?.fragments.materialFields.bookmark?.bookmarked == false){
                self.bookmarkButton.setImage(#imageLiteral(resourceName: "bookmarkEmpty"), for: .normal)

            } else if (self.materials?.fragments.materialFields.bookmark?.bookmarked == true) {

                self.bookmarkButton.setImage(#imageLiteral(resourceName: "Bookmark Filled"), for: .normal)  
            }  

 func setMaterials(materials: GetMaterialsByTypeQuery.Data.AllMaterial) {
        self.materials = materials
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        self.bookmarkButton.setImage(nil, for: .normal)
    }

关于如何解决这个问题的任何想法?

先感谢您。

当您重复使用单元格时,就会发生这种情况。 只需在设置材料时在单元格中设置书签相关数据并基于此设置图像。 它会工作得很好。

尝试这个

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let itemCell = tableView.dequeueReusableCell(withIdentifier: "NewPodcastTableViewCell", for: indexPath) as! NewPodcastTableViewCell

        let allMaterial = materials[indexPath.row]
        itemCell.setMaterials(materials: allMaterial)
        itemCell.delegate = self

        if (allMaterial.fragments.materialFields.bookmark?.bookmarked == false){
            self.bookmarkButton.setImage(#imageLiteral(resourceName: "bookmarkEmpty"), for: .normal)

        } else if (self.materials?.fragments.materialFields.bookmark?.bookmarked == true) {

            self.bookmarkButton.setImage(#imageLiteral(resourceName: "Bookmark Filled"), for: .normal)  
        } 
    return itemCell

}

更改设置图像的代码,如下所示

 self.bookmarkButton.setImage(#imageLiteral(resourceName: "Bookmark Filled"), for: .normal)
 if (self.materials?.fragments.materialFields.bookmark?.bookmarked == false){
            self.bookmarkButton.setImage(#imageLiteral(resourceName: "bookmarkEmpty"), for: .normal)

        }

或者只是删除 else if condtion 并使用 just else 条件

  if (self.materials?.fragments.materialFields.bookmark?.bookmarked == false){
            self.bookmarkButton.setImage(#imageLiteral(resourceName: "bookmarkEmpty"), for: .normal)

        } else {

            self.bookmarkButton.setImage(#imageLiteral(resourceName: "Bookmark Filled"), for: .normal)  
        }  

尝试使用 indexpath 在cellForRowAt中设置书签,这样当它滚动时,它将保持正确单元格的轨道

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM