gpt4 book ai didi

Swift:在 CustomCell 中处理开关

转载 作者:行者123 更新时间:2023-11-28 06:37:26 24 4
gpt4 key购买 nike

我想处理我的开关。如果 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
}

这里是 switch 的函数。

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() 重新加载表

希望对您有所帮助。

jack

关于Swift:在 CustomCell 中处理开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38812522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com