Ios UITableViewCell的选定按钮在滚动时消失
我正在处理一个用swift 3编码的项目,在UITableViewCell中有一个UIButton,点击按钮后图像会发生变化。尽管所选按钮按预期选择,但一旦UITableview滚动,所选图像将消失,因为单元格已被重复使用。因为我是编程新手,写逻辑有困难。帮助者将非常感激下面的代码 塞尔福罗Ios UITableViewCell的选定按钮在滚动时消失,ios,swift,uitableview,swift3,Ios,Swift,Uitableview,Swift3,我正在处理一个用swift 3编码的项目,在UITableViewCell中有一个UIButton,点击按钮后图像会发生变化。尽管所选按钮按预期选择,但一旦UITableview滚动,所选图像将消失,因为单元格已被重复使用。因为我是编程新手,写逻辑有困难。帮助者将非常感激下面的代码 塞尔福罗 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
//Button_Action
addSongButtonIdentifier(cell: cell, indexPath.row)
}
这是创建单元的地方
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)
}
点击功能
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
内设置所有这些属性
相反,您应该先对每个单元执行一次,就像在故事板本身中或通过提供模型到单元类一样。您需要在控制器级别保持按钮选择状态。您需要更改用于配置tableview的模型 我创建了一个类似的场景。我使用数组
selectionStatusArray
来维护按钮的选择状态
示例:
1UIViewController
包含UITableView
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
}
}
}
class TableViewCell: UITableViewCell
{
@IBOutlet weak var addButton: UIButton!
var tapHandler: ((Int)->())?
@IBAction func tapFunction(_ sender: UIButton)
{
sender.isSelected = !sender.isSelected
tapHandler?(sender.tag)
}
}
2。自定义UITableViewCell
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
}
}
}
class TableViewCell: UITableViewCell
{
@IBOutlet weak var addButton: UIButton!
var tapHandler: ((Int)->())?
@IBAction func tapFunction(_ sender: UIButton)
{
sender.isSelected = !sender.isSelected
tapHandler?(sender.tag)
}
}
您可以根据需要配置
UITableViewCell
。一旦tableview重新加载,您的选择将消失,因为您需要在数据集中存储选定状态,而不是按钮状态。您需要存储选定项的索引或行号。我知道原因,但在写入逻辑时遇到问题,一段代码片段会非常欣赏您的代码是正确的,但您使用它的方式有点错误,因此,如果您演示如何调用函数addSongButtonIdentifier,我可以建议您一种正确的方法。