简体   繁体   English

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

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

I have an issue while scrolling in the tableView.在 tableView 中滚动时出现问题。 I implemented the bookmarking in the project, and everything works perfectly, except when scrolling.我在项目中实现了书签,一切正常,除了滚动时。 As it runs for the first time, it calls the API to check if the item is bookmarked or not.当它第一次运行时,它会调用 API 来检查该项目是否已添加书签。 If bookmarked, show it displays the filled image, if not, displays the not filled image.如果加了书签,则显示它显示填充的图像,如果没有,则显示未填充的图像。 It works fine when it loads, for the first time, but when I scroll the bookmark button image changes.第一次加载时它工作正常,但是当我滚动书签按钮图像时会发生变化。

I tried to use prepareForReuse on CustomTableViewCell to solve the issue, and also render the button image on the cellforRow only but didn't work.我尝试在 CustomTableViewCell 上使用 prepareForReuse 来解决这个问题,并且只在 cellforRow 上渲染按钮图像,但没有奏效。

Here is the MainViewController:这是主视图控制器:


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)")
                }
           }
    }
}

And here is the CustomTableViewCell这是 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)
    }

Any idea on how to fix the issue?关于如何解决这个问题的任何想法?

Thank you in advance.先感谢您。

That happens when you reuse the cells.当您重复使用单元格时,就会发生这种情况。 Just set the bookmark related data in the cell while you are setting the materials and set the image based on that.只需在设置材料时在单元格中设置书签相关数据并基于此设置图像。 It would work fine.它会工作得很好。

Try This尝试这个

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 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

} }

Change your code of setting image like below更改设置图像的代码,如下所示

 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)

        }

Or just remove else if condtion and use just else condition或者只是删除 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)  
        }  

Try to set the bookmark inside cellForRowAt using indexpath so when its scroll it will keep the track of correct cell尝试使用 indexpath 在cellForRowAt中设置书签,这样当它滚动时,它将保持正确单元格的轨道

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

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