swift - Switch 中 TableView 中的 UISwitch

标签 swift tableview uiswitch

各位程序员大家好!我有一个挑战需要帮助。我已经使用自定义样式单元格构建了一个表。

这个单元格只有一个 LabelUISwitch。标签显示名称,开关显示他们是否是管理员。这非常有效。我的挑战是当开关改变时,我如何以及在哪里放置代码以使用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/

相关文章:

ios - 首次加载时 UITableViewCell 变窄

iOS UITableViewCell EditingStyle 以编程方式设置检查(ON)

java - 在动态 TableView 中编辑单元格值?

ios - 如何在 SWIFT 4 中保存 UISwitch 的状态?

ios - 按钮开关更改默认颜色

iphone - UISwitch - 从开/关更改为是/否

ios - 有没有办法为不同的群体推送通知

swift - 为什么这个谓词格式会变成 '= nil'

arrays - 警告 "Simultaneous accesses to parameter ' self' ..."真的适用于此吗?

iphone 如何使用 sqlite 获取组部分 tableview 的姓氏首字母?