Swift:在CustomCell中处理开关

时间:2016-08-07 08:58:25

标签: swift tableview uiswitch

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

稍后我想在第一个开关改变时隐藏一个部分。那我怎么能在这个功能中做到这一点?

public:
/// performs a fast insert without range checking. Range-check yourself before!
void checkedInsert(Element* element)
{
    insertWithCheckOption(element, true);
}

/// performs a range-checked insert
void uncheckedInsert(Element* element)
{
    insertWithCheckOption(element, false);
}

private:
/// implements insert with range check option
void insertWithCheckOption(Element* element, bool doRangeCheck)
{
    // ... do before code portion ...
    if (doRangeCheck) {
         // do expansive range checking  ...
    }
    // ... do after code portion ...
}

这里是switch的功能。

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
}

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可能希望将以下代码移至ViewDidLoad

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

然后在switchDidChangegoingSwitch()

重新加载表格

希望它有所帮助。

杰克