自定义单元格上的按钮切换背景图像状态,由其他单元格重用,应该是重用问题,但不知道如何解决?你能给我一些建议吗?
@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;
}();
在UITableViewCell的override方法中
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
P.S您可以在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)
}
这解决了单元上的按钮选择状态重用问题
这应该是这样的,
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()
}