How to create a UITableViewCell with a UISwitch and get the data?

﹥>﹥吖頭↗ 提交于 2019-11-28 17:28:52

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];
}

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



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

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
    }
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!