8

I saw this question:

UISwitch in a UITableView cell

But it seemed to be dealing with a dynamic page. I'm really just trying to create a settings page for my app and a few of the cells in the table (not all of them), require switches on the table cell. How could I do this?

Community
  • 1
  • 1
aerain
  • 1,149
  • 3
  • 12
  • 17

4 Answers4

23

Here's how it works in Interface Builder. No need for code.

  • Drag a UISwitch into your View Controller. It needs to be outside the main view. Kinda just chillin' there with some other things.

  • Ctrl-Click on the table view cell that should have this switch, and drag to the new Switch. See 1. enter image description here

  • Connect the switch to the table view cell's accessoryView outlet. See 2. enter image description here

Run the app and watch the switch appear in the table - done!

Note: the switch oddly does not show up in Interface builder. But it will show up when you run the app

If you also want to hook it up to an instance variable - for example so you can read its "on" property later - connect it to an outlet just like you would any other view 3, 4.

enter image description here enter image description here

PS: Keep in mind that UISwitch on/off state is in its "on" property, not "selected". I am just adding this because it's kinda confusing.

PPS: Sorry for the oversized pix, I'm on a retina screen.

n13
  • 6,843
  • 53
  • 40
  • This looks promising.. but after the 2nd step and on the 'This will make the switch appear in the table - done!' part I don't see the switch appear in the table. I'm pretty sure I followed the steps correctly as I see the dark popup Outlets - accessoryView appear. Any clue? – Bruce Aug 12 '15 at 09:07
  • Well - run the app - do you see the switch? I just realized it doesn't show up in Interface Builder. But it will be in the cell when you run the app. Try it Once it works for you I'll update the wording up there. – n13 Aug 12 '15 at 09:16
  • You are right, it appears at just the right place I want it to be. Awesome! I'm also figuring out how to hook the switch to run some code whenever it is flipped. Any pointers? – Bruce Aug 12 '15 at 09:21
  • Yes. Use the assistant editor and Ctrl-drag from the switch to your view controller class (.h file for Obj-C, swift file for swift). You then get the option to create either an outlet, e.g. variable holding that switch, or to create an action. You want to create an action. – n13 Aug 12 '15 at 09:24
  • I just figured how silly my question was.. so it is just like any UI component and we can freely assign outlets and actions to it. Thank you so much! – Bruce Aug 12 '15 at 09:32
  • 2
    This is a very helpful response, too bad it hasn't got more upvotes. – eagerMoose Nov 24 '15 at 19:46
  • But if I have two rows I need two "switches" (or whatever view I am using) - in other words it seems I can't share one switch with several rows, right? – Jacob Rohde May 30 '18 at 09:30
  • Nice! This is exactly what I need. – hstdt Nov 02 '19 at 05:55
5

Just drag a UISwitch over to the cell that you have laid out. You can then use the assistant editor (which is awesome) to wire it up to the parent class. One tip I will give ya, if you are planning several cells that will be similar in appearance, create the first one in the section and lay it out just right, then you can increase the # of rows in that section and IB will create copies of the existing row for you.

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • 8
    note that to do drag a UISwitch element onto the static UITableViewCell while storyboarding, the cell must be of style "Custom" – Archie1986 Aug 07 '13 at 16:16
  • @Archie1986 It works for "Basic" cells as well, albeit with some coercion. – Ja͢ck Jan 20 '15 at 20:23
  • The video link is broken, btw. – Ja͢ck Jan 20 '15 at 20:23
  • I did it exactly like this but had problem because the on state of m IBOutlet did not change. That is why I found this question. For me it helped using the on property of the sender not of my IBOutlet. It might help someone. – dehlen Jan 24 '16 at 00:50
  • it just puts cell item to the center above uilabel – user924 Jun 16 '19 at 17:29
  • @Ja͢ck then shows us, yes, you can add it but you can't move to the right place – user924 Jun 16 '19 at 17:30
2

I create a function for this case and its work good for me .. try it , first in the cell creation you could check the row you want to add a UISwitch in , for ex.

 if(indexPath.row == 0)
      [self createOnOffView:cell withTitle:@"Somthing"  withTag:1001 defaultVal:YES];

And the function is :

- (void) createOnOffView:(UITableViewCell*) cell withTitle:(NSString*) title withTag:(int)tag defaultVal:(BOOL) defaultVal
{
    CGRect rect;

    cell.textLabel.text = title;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    rect = cell.contentView.frame;

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        rect.origin.x = cell.frame.size.width - 20;
    else
        rect.origin.x = rect.size.width/2 +35;

    rect.origin.y = rect.size.height/2 - 15;    
    rect.size.width = 60;    

    UISwitch *switchView = [[UISwitch alloc] initWithFrame:rect];
    [cell.contentView addSubview:switchView];
    [switchView addTarget:self action:@selector(didChangeSwitch:) forControlEvents:UIControlEventValueChanged];

    switchView.tag = tag;

    [switchView setOn:defaultVal];

    [switchView release];
}

And when the value is switch is change this method will be fired .. so you can know which switch based on the tag

- (void) didChangeSwitch:(UISwitch*)switchView
{
    if(switchView.tag == 1001)
    {
       //Do Somthing
    }

    if(switchView.tag == 1002)
    {
        //Do Somthing
    }    
}

Hope this will be helpful :)

Malek_Jundi
  • 6,140
  • 2
  • 27
  • 36
  • 1
    This is nowhere near as easy as simply dragging the switch over to the cell you want it to be in using Interface Builder. Have a look at how the Static Cell tableivews work. No need for any datasource methods when using these, you just wire up IBActions for objects just like in a regular ViewController. – LJ Wilson May 29 '12 at 13:00
0

You do it exactly the same way as you do in the question you linked to. There is no such thing as a "dynamic" table. The table just displays your model. Your model might be dynamic, but the tableview is not. Either way, follow the answer to that question and you will have a switch in your tableview.

sosborn
  • 14,676
  • 2
  • 42
  • 46
  • This is not correct. Starting in iOS 5, there are static cell tableviews that do not need any datasource, by contrast, Dynamic Prototype cells need a datasource of some kind. – LJ Wilson May 29 '12 at 11:41
  • Fair enough. Wasn't aware of that. After reading the docs though it still seems easier to just use cellForRowAtIndexPath. – sosborn May 29 '12 at 12:47
  • 2
    See video link in my edited answer, I think you will change your mind about how easy it is to add objects to a SC TV – LJ Wilson May 29 '12 at 13:31
  • I guess as with all things, YMMV. For me, I look at it as a couple lines of code that can be done in the same amount of time as it was done in your video. Anyway, I appreciate the video. Very nice. – sosborn May 30 '12 at 00:01
  • Fair enough. Thanks for understanding a different viewpoint. As with most programming methods, there are several ways to accomplish the same thing. – LJ Wilson May 30 '12 at 00:26