Question

I have a simple problem. I created a custom UITableViewCell that includes a UISwitch in Interface Builder.

Question 1: Easier, better way to do it ? Question 2: When used in an UITableViewController, what would be the best way to get the data ? At some point I either need to go through the table and check every cell about the status OR send some feedback when the status of the switches changes directly ?

Thanks for the help.

Was it helpful?

Solution

You could just add the UISwitch in your accessory view. That is the easiest way to do it, not to mention that it looks 'elegant'.

Then, in your tableview controller code, you could just call a selector every time the switch is toggled, or even toggle the switch itself by getting the switch's current status in your controller.

Let know if you'd like a code sample.

---sample code---

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    //add a switch
    UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = switchview;
    [switchview release];
}

cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];

return cell;
}

Then, you could have a method that updates a switch based on changes in your model. You could use anything you want - delegates, notifications, KVO, etc.

Example:

- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UISwitch *switchView = (UISwitch *)cell.accessoryView;

if ([switchView isOn]) {
    [switchView setOn:NO animated:YES];
} else {
    [switchView setOn:YES animated:YES];
}

 }

OTHER TIPS

Switch.tag = indexPath.row;
[Switch addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];



- (void)updateSwitchAtIndexPath:(UISwitch *)aswitch{
    FFLog(@"%i",aswitch.tag);
}

Thanks. I don't have your indexPath, but you can identify a cell or object with a tag like This:

if (indexPath.row == 0) {
        cell.textLabel.text = @"Twitter";


        UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
        switchview.tag = 111;
        //switchview.selected = 0;
        cell.accessoryView = switchview;

        [switchview addTarget:self action:@selector(updateSwitchAtIndexPath) forControlEvents:UIControlEventTouchUpInside];

        //cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
        //[switchview release];

    }


- (void)updateSwitchAtIndexPath {

for (UITableViewCell * cell in tblView.subviews) {

   for (UISwitch *swch in cell.subviews) {


         UISwitch *switchView = (UISwitch *)cell.accessoryView;

        if (switchView.tag == 111) {

            if ([switchView isOn]) {
                 NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ON");
            }else{
                NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> OFF");
            }

        } 

          if (switchView.tag == 222) {
            if ([switchView isOn]) {
                NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ON");
            }else{
                NSLog(@")>>>>>>>>>>>>>>>>>>>>>>>>>>>>> OFF");
            }

        }
    }
}

}

Two options:

1) initialize your cell with a pointer to your data model. Have the cell implement the delegate callback for the switch. When the switch changes, use the pointer to your data model to reflect the new setting. Alternatively, initialize the cell with a pointer to your view-controller. Have the cell implement the switch delegate method, then call back into your view controller with the appropriate cell context info.

2) have your view-controller implement the switch delegate callback. When the switch changes, determine which switch/cell it was (may have to set the switch's "tag" property to be a row number, or something) and handle the update in the context of your view controller.

To get the value of the UISwitch in Swift 3 and Swift 4 you can add a target to it:

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {

    ...

    cell.uiSwitch.tag = 100
    cell.uiSwitch.addTarget(self, action: #selector(ViewController.switchAtValueChanged(switchFromTableViewCell:)), for: UIControlEvents.valueChanged)

    ...
}

And you can get the value like this:

@objc func switchAtValueChanged(uiSwitch: UISwitch) {

    if uiSwitch.tag == 100 {
        let value = uiSwitch.isOn
    }
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top