滚动时,UITableViewCell的选定button会消失

我正在使用swift 3编码的项目,我有一个UITableViewCell里面的UIButton,在点击button后图像会改变。 虽然所选的button被按照预期select,但是一旦UITableview滚动select的图像就会消失,因为这些单元已被重新使用。 由于我刚刚接触编程时遇到了编写逻辑的麻烦。 帮助将非常感谢代码如下。

CellFoRow

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) //Button_Action addSongButtonIdentifier(cell: cell, indexPath.row) } 

这是单元格被创build的地方。

 func addSongButtonIdentifier(cell: UITableViewCell, _ index: Int) { let addButton = cell.viewWithTag(TABLE_CELL_TAGS.addButton) as! UIButton //accessibilityIdentifier is used to identify a particular element which takes an input parameter of a string //assigning the indexpath button addButton.accessibilityIdentifier = String (index) // print("visible Index:",index) print("Index when scrolling :",addButton.accessibilityIdentifier!) addButton.setImage(UIImage(named: "correct"), for: UIControlState.selected) addButton.setImage(UIImage(named: "add_btn"), for: UIControlState.normal) addButton.isSelected = false addButton.addTarget(self, action: #selector(AddToPlaylistViewController.tapFunction), for:.touchUpInside) } 

轻拍function

 func tapFunction(sender: UIButton) { print("IndexOfRow :",sender.accessibilityIdentifier!) // if let seporated by a comma defines, if let inside a if let. So if the first fails it wont come to second if let if let rowIndexString = sender.accessibilityIdentifier, let rowIndex = Int(rowIndexString) { self.sateOfNewSongArray[rowIndex] = !self.sateOfNewSongArray[rowIndex]//toggle the state when tapped multiple times } sender.isSelected = !sender.isSelected //image toggle print(" Array Data: ", self.sateOfNewSongArray) selectedSongList.removeAll() for (index, element) in self.sateOfNewSongArray.enumerated() { if element{ print("true:", index) selectedSongList.append(songDetailsArray[index]) print("selectedSongList :",selectedSongList) } } } 

你有这个代码addButton.isSelected = false这是造成这个问题,因为我相信你是在tableView delegate方法内调用函数addSongButtonIdentifier ,你不应该在tableView delegate内设置所有这些属性。

相反,你应该做的最初,只有一次,为每个单元格,如故事板本身或通过提供一个单元格类的模型。

您需要维护控制器级别的buttonselect状态。 你需要改变你用来configuration你的tableview的模型。

我创build了一个类似的场景。 我使用了一个数组selectionStatusArray来维护button的select状态。

例:

1.包含UITableView UIViewController

 class ViewController: UIViewController, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var selectionStatusArray = [false, false, false, false, false] //Array that maintains the button selection status func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return selectionStatusArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell addSongButtonIdentifier(cell: cell, indexPath.row) return cell } func addSongButtonIdentifier(cell: TableViewCell, _ index: Int) { cell.addButton.tag = index cell.addButton.isSelected = selectionStatusArray[index] cell.tapHandler = { self.selectionStatusArray[$0] = cell.addButton.isSelected } } } 

2.自定义UITableViewCell

 class TableViewCell: UITableViewCell { @IBOutlet weak var addButton: UIButton! var tapHandler: ((Int)->())? @IBAction func tapFunction(_ sender: UIButton) { sender.isSelected = !sender.isSelected tapHandler?(sender.tag) } } 

您可以根据您的要求configurationUITableViewCell