[英]Why is that? Button toggles the state on a custom cell whose background image is reused by other cells
自定義單元格上的按鈕切換背景圖像狀態,其他單元格已重用該按鈕,應該是重用問題,但不知道該如何解決? 你能給我一些建議嗎?
@objc func LickCheck(_ sender:UIButton){
//Toggle button background image
if !sender.isSelected {
//code
}else{
//code
}
sender.isSelected = !sender.isSelected
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : FoneCell = tableView.dequeueReusableCell(withIdentifier: "Focid", for: indexPath) as! FoneCell;
cell.xxBtn.tag = indexPath.row;
cell.xxBtn.addTarget(self, action: #selector(LickCheck), for: UIControl.Event.touchUpInside);
return cell;
}
FoneCell.swift:
lazy var xxxBtn : UIButton = {
let btn = UIButton();
btn.setImage(UIImage.init(named: "love_18x18_"), for: UIControl.State.normal);
btn.setImage(UIImage.init(named: "love_on_20x20_"), for: UIControl.State.selected)
return btn;
}();
這應該是這樣的,
FoneCell.swift :
protocol FoneCellDelegate: class {
func didSelectButton(atIndex: Int)
}
@IBOutlet var loveBtn: UIButton!
public weak var delegate: FoneCellDelegate?
func awakeFromNib() {
super.awakeFromNib()
loveBtn.addTarget(self, action: #selector(LickCheck), for: UIControl.Event.touchUpInside);
}
@objc func LickCheck(_ sender:UIButton){
//Toggle button background image
if !sender.isSelected {
addDate(sender.tag)
}else{
deleteDate(sender.tag)
}
sender.isSelected = !sender.isSelected
delegate?.didSelectButton(atIndex: sender.tag)
}
ViewController.swift
var buttonStates = Array(repeating: false, count: 10)//if you need 10 rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return buttonStates.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : FoneCell = tableView.dequeueReusableCell(withIdentifier: "Focid", for: indexPath) as! FoneCell;
cell.loveBtn.tag = indexPath.row;
cell.loveBtn.isSelected = buttonStates[indexPath.row]
cell.delegate = self // Implement protocol on cell's class. And update the value in buttonStates when state toggled
return cell
}
func didSelectButton(atIndex: Int) {
buttonStates[atIndex] = !buttonStates[atIndex]
tableView.reloadData()
}
在UITableViewCell的重寫方法中
override func prepareForReuse() {
super.prepareForReuse()
//set default state here
self.imageView.image = nil
self.toggleButton.isOn = false
//.....
}
或者您可以在tableViewcellForRowAtIndexPath中執行相同操作
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
//Set default behaviour here
cell.imageView.image = nil
cell.toggleButton.isOn = false
....
return cell
PS,您可以在viewController中創建數組以保存所選按鈕的狀態。 例如
let isSelectedArray = Array(repeating: false, count: 100)
@objc func LickCheck(_ sender:UIButton){
//Toggle button background image
let tag = sender.tag
if !isSelectedArray[tag]{
//code
}else{
//code
}
isSelectedArray[tag] = !isSelectedArray[tag]
sender.isSelected = !sender.isSelected
}
我已經解決了這個問題,首先,定義一個單擊按鈕的全局記錄數組
lazy var numbercells:[Int] = []
其次,在
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : FoneCell = tableView.dequeueReusableCell(withIdentifier: "Focid", for: indexPath) as! FoneCell;
if self.numbercells.contains(indexPath.row){
cell.loveBtn.isSelected = true;
}else{
cell.loveBtn.isSelected = false;
}
cell.loveBtn.addTarget(self, action: #selector(LickCheck), for: UIControl.Event.touchUpInside);
cell.loveBtn.tag = indexPath.row;
return cell;
}
最后
@objc func LickCheck(_ sender:UIButton){
if !sender.isSelected {
self.numbercells.append(sender.tag);
addDate(sender.tag)
}else{
self.numbercells = self.numbercells.filter(){$0 != sender.tag}
deleteDate(sender.tag)
}
let posinton = IndexPath(row: sender.tag, section: 0);
self.tableView.reloadRows(at: [posinton], with: UITableView.RowAnimation.none)
}
這解決了單元格上的按鈕選擇狀態重用問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.