Tableviewcell中的UISwitch状态在用户滚动时重置--Swift

时间:2018-06-02 11:26:05

标签: uitableview swift3 uiswitch

我已经在这个问题上寻找解决方案,但似乎没有一个能用于我的用例。

我在viewcontroller中有一个表,我面临的问题是当滚动UISwitch状态时会重置为OFF。我理解表格单元格可以重复使用,但是当用户根据下面的代码滚动时,如何实现一个能够恢复UISwitch状态的解决方案

import UIKit

class StirrViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var mylabel: UILabel!
var myString = String()

@IBAction func stirrBtn(_ sender: AnyObject) {
}

var timeSelected = String()
var selectedTimeArr = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    mylabel.text = myString
    self.timeSelected = myString
}

func switchChanged(_ sender : UISwitch!){
    print("table row switch Changed \(sender.tag)")
    print("The switch is \(sender.isOn ? "ON" : "OFF")")
    let kValue = (sender.tag + 1)
    let keyValue = String(kValue)
    if sender.isOn {
        recipeSettings.boolStirrSwitch[keyValue] = true
        recipeSettings.switchedOnArr.append(keyValue)

    } else {
        recipeSettings.boolStirrSwitch[keyValue] = false
    }
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    let stringNum = Int(self.timeSelected)
    recipeSettings.recipeTimeSet2 = stringNum!
    return(stringNum)!
}


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UITableViewCell
    //here is programatically switch make to the table view
    let switchView = UISwitch(frame: .zero)
    switchView.setOn(false, animated: true)
    switchView.tag = indexPath.row // for detect which row switch Changed
    switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
    cell.accessoryView = switchView

    // Process data displayed in rows(minutes)
    let endTime = Int(self.timeSelected)
    let startTime = Int(1)

    // Recipe time array
    let timeArray: [Int]  = Array(startTime...endTime!)
    let stringTimeArr = timeArray.map{String($0)}

    // Save time array to global variable
    recipeSettings.recipeTimeSetArr = stringTimeArr

    // Create a boolean Array to hold all default false booleans
    let defBool: Bool = false
    var defBoolArr: [Bool] = []

    // Fill the array with the defaults boolean
    for _ in 0..<stringTimeArr.count{defBoolArr.append(defBool)}

    // Map the array to global dictionary containing the Time in an array and default "false" value

    for i in 0..<stringTimeArr.count {
        recipeSettings.boolStirrSwitch[stringTimeArr[i]] = defBoolArr[i]
    }

    // Add the minutes to cell table
    cell.textLabel?.text = stringTimeArr[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

正如您在我的代码中看到的,我确实将每个UI开关的状态保存在全局变量字典中。如何根据此代码解决UISwitch更改状态的问题?所有帮助表示赞赏。提前致谢

1 个答案:

答案 0 :(得分:1)

var switchState = [String : Bool]()
  • 你的recipeSettings.boolStirrSwitch应该是这样的。
  • 当您使用timeSelected作为numberOfRowsInSection时显示 你的cell.textLabel,所以你不需要额外的stringTimeArr 为此。
  • 您在cellForRowAt中所做的所有处理都将再次发生 再次使用表格单元格以便设置数据在另一个表单中执行 function然后reload TableView

问题的解决方案应该是这样的。

import UIKit

class StirrViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

//make tableView IBOutlet for reloading data
@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var mylabel: UILabel!
var myString = String()

@IBAction func stirrBtn(_ sender: AnyObject) {
}

var timeSelected = String()
var selectedTimeArr = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    mylabel.text = myString
    self.timeSelected = myString
    self.setdefaultSwitchState()
}

//recipeSettings.boolStirrSwitch should be decleard like that
var switchState = [String : Bool]()

//setDeaultSwitchState
func setdefaultSwitchState(){
    if let timeSelected = Int(self.timeSelected){
        for value in 0..<timeSelected{
            switchState["\(value)"] = false
            //or
            //recipeSettings.boolStirrSwitch["\(value)"] = false
        }
    }
    self.tableView.reloadData()
}


@objc func switchChanged(_ sender : UISwitch!){
    print("table row switch Changed \(sender.tag)")
    print("The switch is \(sender.isOn ? "ON" : "OFF")")
    let kValue = (sender.tag + 1)
    let keyValue = String(kValue)
    if sender.isOn {
        switchState[keyValue] = true

    } else {
        switchState[keyValue] = false
    }
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    let stringNum = Int(self.timeSelected)
    recipeSettings.recipeTimeSet2 = stringNum!
    return(stringNum)!
}


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UITableViewCell
    //here is programatically switch make to the table view
    let switchView = UISwitch(frame: .zero)
    switchView.setOn(false, animated: true)
    switchView.tag = indexPath.row // for detect which row switch Changed
    switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
    cell.accessoryView = switchView


    cell.textLabel?.text = "\(indexPath.row + 1)"
    if let switchState = switchState["\(indexPath.row)"] {
        if switchState{
            switchView.isOn = true
        }else{
            switchView.isOn = false
        }
    }else{
        switchView.isOn = false
    }

    return cell
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}