Swift:自定义单元格中的手柄开关

Swift:自定义单元格中的手柄开关,swift,tableview,uiswitch,Swift,Tableview,Uiswitch,我想处理我的开关。如果UIswitches发生更改,如何保存数据?如果发生更改,如何更新我的tableview 稍后,我想在更改第一个开关时隐藏一个部分。那么,在这个函数中,我该怎么做呢 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell: UITableViewCell! let row = i

我想处理我的开关。如果UIswitches发生更改,如何保存数据?如果发生更改,如何更新我的tableview

稍后,我想在更改第一个开关时隐藏一个部分。那么,在这个函数中,我该怎么做呢

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell: UITableViewCell!
    let row = indexPath.row


    ///set switch in cells
    goingSwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
    goingSwitch.addTarget(self, action: #selector(ViewController.switchDidChangegoingSwitch(_:)), forControlEvents: .ValueChanged)


    eventDaySwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
    eventDaySwitch.addTarget(self, action: #selector(ViewController.switchDidChangeEventDaySwitch(_:)), forControlEvents: .ValueChanged)



    ////// Cells
    switch(indexPath.section){
    case 0:
        cell = tableView.dequeueReusableCellWithIdentifier("detailCell", forIndexPath: indexPath)
        if row == 0 {
            cell.textLabel?.text = "Eventname: xxxx"
            cell.detailTextLabel?.text = "okay ausgabe"
        }
        if row == 1 {
            cell.textLabel?.text = "Eventdate: xxxxxxxx"
            cell.detailTextLabel?.text = "Eventdate textausgabe"
        }
    case 1:
        cell = tableView.dequeueReusableCellWithIdentifier("goingCell", forIndexPath: indexPath)
        if row == 0 {
            cell.textLabel?.text = "Can you Go to Event? ???"
            cell.accessoryView = goingSwitch
        }
        if row == 1 {
            cell.textLabel?.text = "Is Going: \(guysareGoing)"

        }

    case 2:
        cell = tableView.dequeueReusableCellWithIdentifier("daysCell", forIndexPath: indexPath)
        cell.textLabel?.text = eventdays[row]
        cell.accessoryView = eventDaySwitch
    case 3:
        cell = tableView.dequeueReusableCellWithIdentifier("adminCell", forIndexPath: indexPath)
        cell.textLabel?.text = others[row]
    default:
        break
    }


    // return cell
    return cell
}
这里是开关的功能

func switchDidChangegoingSwitch(sender:UISwitch!)
{


    if (sender.on == true){
        print("goingSwitch is on")
    }
    else{
        print("goingSwitch is off")
    }


}

感谢您的帮助

您可能希望将下面的代码移动到ViewDidLoad

goingSwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
goingSwitch.addTarget(self, action: #selector(ViewController.switchDidChangegoingSwitch(_:)), forControlEvents: .ValueChanged)
然后在
switchDidChangegoingSwitch()

希望能有帮助


杰克

谢谢你的这个技巧,但是如果我把它移到viewdidload,什么都不会显示。