UISwitch in tableview in switch

Hi fellow programmers! I have a problem that I need help with. I built a table using a special style cell.

This cell just has Labeland UISwitch. The name is displayed on the label, and a switch indicates whether they are an administrator or not. It works great. My problem is how and where can I put the code in response when the switch changes.

So, if I click on the switch to change it from place to place, where can I print the person’s name? If I can get the name to print, I can make the php / sql code myself. Thanks, and here is a snippet of 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
}
+4
source share
3

, UISwitch , . :

class CustomTableViewCell: UITableViewCell {

     @IBOutlet weak var label: UILabel!

     @IBAction func statusChanged(sender: UISwitch) {
         self.label.text = sender.on ? "On" : "Off"
     }
}

UILabel UISwitch, , . , .

+7

. ValueChanged UISwitch, YOUR_CUSTOM_CELL, . "" .

0

,

- tableview UISwitch /.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/index.html#//apple_ref/occ/instm/UIControl/addTarget:action:forControlEvents :

actiontells the UISwitch instance which method it should call when the user switches to the switch. targettells the UISwitch instance what object this method is hosted on.

Generally, you should use a subclass of UITableViewController (or UIViewController) as the target.

0
source

All Articles