Swift: CustomCell 中的句柄开关
Swift: Handle Switch in CustomCell
我想处理我的 Switch。如果 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()
重新加载 table
希望对您有所帮助。
杰克
我想处理我的 Switch。如果 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()
希望对您有所帮助。
杰克