gpt4 book ai didi

ios - 在 TableView 上滚动时 tableViewCell 中的按钮图像发生变化

转载 作者:行者123 更新时间:2023-11-28 13:27:59 25 4
gpt4 key购买 nike

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

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

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

这里是 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)
}

关于如何解决这个问题有什么想法吗?

提前谢谢你。

最佳答案

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

关于ios - 在 TableView 上滚动时 tableViewCell 中的按钮图像发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58010173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com