在 UITableViewCell 中识别 UISwitch
identifying a UISwitch in UITableViewCell
我在 UITableViewCell 中实现 UISwitch 时遇到了一些问题。我的 tableViewCell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = "Hello Magic Switch buyers!"
cell.textLabel?.textColor = UIColor.whiteColor()
cell.backgroundColor = UIColor.clearColor()
lightSwitch = UISwitch(frame: CGRectZero) as UISwitch
lightSwitch.on = false
lightSwitch.addTarget(self, action: "switchTriggered", forControlEvents: .ValueChanged );
cell.accessoryView = lightSwitch
return cell
}
这将创建开关,一切正常,直到函数被调用为止。您通常使用按钮(例如按钮)所做的只是使用它 indexPath.row 来区分所有细胞,但由于这是一个附属视图,我无法让它工作! switchTriggered 函数:
func switchTriggered() {
if lightSwitch.on {
let client:UDPClient = UDPClient(addr: "192.168.1.177", port: 8888)
let (_) = client.send(str: "HIGH" )
print(String(indexPath.row) + " set to high")
} else {
let client:UDPClient = UDPClient(addr: "192.168.1.177", port: 8888)
let (_) = client.send(str: "LOW" )
print(String(indexPath.row) + " set to low")
}
}
该函数不知道切换了哪个 lightSwitch 以及 indexPath 是什么。我该如何解决这个问题?如果它是一个按钮,我可以使用 accessoryButtonTappedForRowWithIndexPath
但事实并非如此。
一些帮助将不胜感激,因为有关 TableViewCells 中 UISwitch 的所有信息都在 Objective-C。
非常感谢!
最简单的解决办法是使用tag
属性的开关。创建开关时,分配一个标签
lightSwitch = UISwitch(frame: CGRectZero) as UISwitch
lightSwitch.tag = 2000
lightSwitch.addTarget(self, action: Selector("switchTriggered:"), forControlEvents: .ValueChanged );
然后修改您的处理程序方法以具有发送者参数
func switchTriggered(sender: AnyObject) {
let switch = sender as! UISwitch
if switch.tag == 2000 {
// It's the lightSwitch
}
}
我在 UITableViewCell 中实现 UISwitch 时遇到了一些问题。我的 tableViewCell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = "Hello Magic Switch buyers!"
cell.textLabel?.textColor = UIColor.whiteColor()
cell.backgroundColor = UIColor.clearColor()
lightSwitch = UISwitch(frame: CGRectZero) as UISwitch
lightSwitch.on = false
lightSwitch.addTarget(self, action: "switchTriggered", forControlEvents: .ValueChanged );
cell.accessoryView = lightSwitch
return cell
}
这将创建开关,一切正常,直到函数被调用为止。您通常使用按钮(例如按钮)所做的只是使用它 indexPath.row 来区分所有细胞,但由于这是一个附属视图,我无法让它工作! switchTriggered 函数:
func switchTriggered() {
if lightSwitch.on {
let client:UDPClient = UDPClient(addr: "192.168.1.177", port: 8888)
let (_) = client.send(str: "HIGH" )
print(String(indexPath.row) + " set to high")
} else {
let client:UDPClient = UDPClient(addr: "192.168.1.177", port: 8888)
let (_) = client.send(str: "LOW" )
print(String(indexPath.row) + " set to low")
}
}
该函数不知道切换了哪个 lightSwitch 以及 indexPath 是什么。我该如何解决这个问题?如果它是一个按钮,我可以使用 accessoryButtonTappedForRowWithIndexPath
但事实并非如此。
一些帮助将不胜感激,因为有关 TableViewCells 中 UISwitch 的所有信息都在 Objective-C。
非常感谢!
最简单的解决办法是使用tag
属性的开关。创建开关时,分配一个标签
lightSwitch = UISwitch(frame: CGRectZero) as UISwitch
lightSwitch.tag = 2000
lightSwitch.addTarget(self, action: Selector("switchTriggered:"), forControlEvents: .ValueChanged );
然后修改您的处理程序方法以具有发送者参数
func switchTriggered(sender: AnyObject) {
let switch = sender as! UISwitch
if switch.tag == 2000 {
// It's the lightSwitch
}
}