gpt4 book ai didi

ios - 防止重复使用的单元格中的按钮再次出现

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

 @objc func addinterestserver(_ sender: UIButton) {

sender.backgroundColor = UIColor.white
sender.setTitleColor(UIColor.black, for: .normal)
sender.setTitle(NSLocalizedString("Added", comment: ""), for: .normal)
sender.isEnabled = false
}



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SearchInterestsCustom

let release = arrayOfRels[(indexPath as NSIndexPath).row]
cell.user.text = release.title
cell.match.text = release.count + NSLocalizedString(" people added", comment: "")
cell.addbutton.tag = release.eventID
cell.addbutton.addTarget(self, action: #selector(SearchInterests.addinterestserver), for: .touchUpInside)
cell.addbutton.layer.cornerRadius = 20
return cell
}

用户点击按钮后,它也会影响其他单元格的按钮,显然我在滚动时多次看到相同的修改按钮。

如何避免这种情况?

最佳答案

您可以通过以下方式避免这种情况

var allSelected = [Int]()

//

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SearchInterestsCustom

if allSelected.contains(release.eventID) {
cell.addbutton.backgroundColor = UIColor.white
cell.addbutton.setTitleColor(UIColor.black, for: .normal)
cell.addbutton.setTitle(NSLocalizedString("Added", comment: ""), for: .normal)
cell.addbutton.isEnabled = false
}
else {
// set default values


}

}

//

 @objc func addinterestserver(_ sender: UIButton) {

sender.backgroundColor = UIColor.white
sender.setTitleColor(UIColor.black, for: .normal)
sender.setTitle(NSLocalizedString("Added", comment: ""), for: .normal)
sender.isEnabled = false

allSelected.append(sender.tag) // store it here
}

关于ios - 防止重复使用的单元格中的按钮再次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50550561/

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