Ios 防止重复使用单元格中的按钮再次出现
用户点击按钮后,它也会影响其他单元格的按钮,很明显,我在滚动时多次看到相同的修改按钮 如何避免这种情况?您可以通过Ios 防止重复使用单元格中的按钮再次出现,ios,swift,uitableview,Ios,Swift,Uitableview,用户点击按钮后,它也会影响其他单元格的按钮,很明显,我在滚动时多次看到相同的修改按钮 如何避免这种情况?您可以通过 @objc func addinterestserver(_ sender: UIButton) { sender.backgroundColor = UIColor.white sender.setTitleColor
@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
}
//
//
在
cellForRowAt
中,将按钮属性设置回所需的值。您需要在您的模型中存储按钮的状态,并在重用单元退出队列时使用该状态设置按钮。
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
}