各位程序员大家好!我有一个挑战需要帮助。我已经使用自定义样式单元格构建了一个表。
这个单元格只有一个 Label
和 UISwitch
。标签显示名称,开关显示他们是否是管理员。这非常有效。我的挑战是当开关改变时,我如何以及在哪里放置代码以使用react。
因此,如果我单击开关将其从关闭更改为打开,我可以在哪里打印人员姓名?如果我能得到要打印的名称,我就可以自己编写 php/sql 代码。谢谢,这是我的代码片段。
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
}
最佳答案
您必须在自定义表格 View 单元格中设置一个action 来处理UISwitch
中的更改并对其中的更改使用react,请参阅以下代码:
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var label: UILabel!
@IBAction func statusChanged(sender: UISwitch) {
self.label.text = sender.on ? "On" : "Off"
}
}
上面的例子只是用来改变UILabel
中关于UISwitch
状态的文本,当然你必须根据你的要求改变它。希望对您有所帮助。
关于swift - Switch 中 TableView 中的 UISwitch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29354969/