Switch 中 TableView 中的 UISwitch [英] UISwitch in TableView in Switch
问题描述
各位程序员好!我有一个需要帮助的挑战.我使用自定义样式单元格构建了一个表格.
Hello fellow programmers! I have a challenge I need help with. I have built a table using a Custom Style Cell.
这个单元格只有一个Label
和UISwitch
.标签显示姓名,开关显示他们是否为管理员.这完美地工作.我的挑战是如何以及在何处放置代码以在更改开关时做出反应.
This cell simply has a Label
and UISwitch
. The label displays a name and the switch displays whether they are an Admin or not. This works perfectly. My challenge is how and where do I put code to react when the switch is changed.
因此,如果我单击开关将其从关闭更改为开启,我在哪里可以让它打印人名?如果我可以打印名称,我可以自己编写 php/sql 代码.谢谢,这是我的代码片段.
So if I click the switch to change it from off to on where can I get it to print the persons name? If I can get the name to print I can do the php/sql code myself. Thanks and here is a snippet from my code.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell
let admin = self.admin[indexPath.row]
let text1a = admin.FirstName
let text1aa = " "
let text1b = admin.LastName
let text1 = text1a + text1aa + text1b
(cell.contentView.viewWithTag(1) as UILabel).text = text1
if admin.admin == "yes" {
(cell.contentView.viewWithTag(2) as UISwitch).setOn(true, animated:true)
} else if admin.admin == "no" {
(cell.contentView.viewWithTag(2) as UISwitch).setOn(false, animated:true)
}
return cell
}
推荐答案
您必须在自定义表格视图单元格中设置 action 来处理 UISwitch
中的更改并对其中的更改做出反应,请参阅以下代码:
You have to set an action in your Custom Table View Cell to handle the change in your UISwitch
and react to changes in it, see the following code :
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var label: UILabel!
@IBAction func statusChanged(sender: UISwitch) {
self.label.text = sender.on ? "On" : "Off"
}
}
上面的例子只是用来改变 UILabel
的关于 UISwitch
状态的文本,当然你必须根据你的要求来改变它.我希望这对您有所帮助.
The above example is just used to change the text of the UILabel
regarding the state of the UISwitch
, you have to change it in base your requirements of course. I hope this help you.
这篇关于Switch 中 TableView 中的 UISwitch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!