Uiswitch 滚动时选择添加到tableView的UI开关

Uiswitch 滚动时选择添加到tableView的UI开关,uiswitch,swift4.1,Uiswitch,Swift4.1,我在UITableView单元格中嵌入了UISwitch override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) if self.users.count &g

我在UITableView单元格中嵌入了UISwitch

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

    if self.users.count > 0 {
        let eachPost = self.users[indexPath.row]
        let postDate = (eachPost.date as? String) ?? ""
        let postTitle = (eachPost.title as? String) ?? ""

        cell.detailTextLabel?.text = postDate
        cell.textLabel?.text = postTitle
    }

    if cell.accessoryView == nil{
        let switchView : UISwitch = UISwitch(frame: .zero)
        switchView.setOn(false, animated: true)
        cell.accessoryView = switchView
    }

    return cell
}

我的桌子有30行。当我在可见单元格上选择开关,然后向下滚动时,默认情况下,在列表底部的单元格上选择开关。如何才能为我的列表选择正确的选项?

我的解决方案而不创建自定义类:

class MyTableViewController: UITableViewController {
var users: [[String: Any]] = [[String: Any]]()
// This array is used for storring the state for switch from each cell. Otherwise when the cell is reused the state is displayed incorrectly
    var switchArray = [Bool]()
 override func viewDidLoad() {
        super.viewDidLoad()
            self.users = result
            for _ in 0 ..< self.users.count {
                self.switchArray.append(false)
            }
            self.tableView?.reloadData()
        }
    }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    let eachPost = self.users[indexPath.row]
    let postTitle = (eachPost.title as? String) ?? ""
    cell.textLabel?.text = postTitle

    let switchView : UISwitch = UISwitch(frame: .zero)
    switchView.isOn = self.switchArray[indexPath.row]
    cell.accessoryView = switchView

return cell}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    self.switchArray[indexPath.row] = true
let switchView : UISwitch = (tableView.cellForRow(at: indexPath)?.accessoryView) as! UISwitch;
    switchView.isOn = true

}

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    self.switchArray[indexPath.row] = false
let switchView : UISwitch = (tableView.cellForRow(at: indexPath)?.accessoryView) as! UISwitch;
    switchView.isOn = false
}
类MyTableViewController:UITableViewController{
变量用户:[[String:Any]]=[[String:Any]]()
//此数组用于存储每个单元的交换机状态。否则,当重新使用该单元时,状态显示不正确
变量switchArray=[Bool]()
重写func viewDidLoad(){
super.viewDidLoad()
self.users=结果
对于0中的uu..UITableViewCell{
let cell=tableView.dequeueReusableCell(带有标识符:“cell”,用于:indexath)
让eachPost=self.users[indexPath.row]
让postTitle=(eachPost.title作为?字符串)?“”“
cell.textLabel?.text=postTitle
let switchView:UISwitch=UISwitch(帧:.0)
switchView.isOn=self.switchArray[indexPath.row]
cell.accessoryView=开关视图
返回单元}
重写func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
self.switchArray[indexPath.row]=true
让switchView:UISwitch=(tableView.cellForRow(at:indexPath)?.accessoryView)作为!UISwitch;
switchView.isOn=true
}
重写func tableView(tableView:UITableView,在indexPath:indexPath中取消行){
self.switchArray[indexPath.row]=false
让switchView:UISwitch=(tableView.cellForRow(at:indexPath)?.accessoryView)作为!UISwitch;
switchView.isOn=false
}

注意:必须启用多选功能

能否添加cellForRowAt函数的完整代码?您需要为indexPath存储更新的开关值,并在cellForRow中设置true/falsefunc@aBilal17我添加了cellForRowAt函数的完整代码。这是因为重用。您必须将值保存在某个数组中,并在行的单元格中设置它们。您能告诉我有关如何执行此操作的更多详细信息吗?谢谢