How to embed UISwitch in a static UITableView?

I saw this question:

UISwitch in UITableView cell

But he seemed to be dealing with a dynamic page. I'm just trying to create a settings page for my application and several of the cells in the table (not all) require inclusion in the table cell. How can i do this?

+6
source share
4 answers

Just drag and drop UISwitch into the cell you laid out. Then you can use the helper editor (which is awesome) to connect it to the parent class. One tip that I will give you if you plan several cells that will be similar in appearance, create the first in this section and put it in the right place, then you can increase the number of rows in this section, and IB will create copies from the existing row for you.

+5
source

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

  • Drag UISwitch to the view controller. It should be outside the main view. Just try something else.

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

  • Plug the switch into an accessory outlet. See 2 . enter image description here

Launch the application and see how the switch appears in the table - done!

Note: the switch strangely does not appear in the interface builder. But it will appear when the application starts

If you also want to connect it to an instance variable, for example, in order to later find out its "on" property - connect it to a socket in the same way as any other type 3 , 4 .

enter image description here enter image description here

PS: Keep in mind that the on / off state of UISwitch is in the "on" property, and not "selected". I just add this because it is a bit confusing.

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

+15
source

I am creating a function for this case, and its work is good for me .. try it, first in creating the cell you can check the line where you want to add UISwitch , for example,

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

And function:

 - (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 a change, this method will be launched. So you can find out which switch is tag based

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

Hope this will be helpful :)

+2
source

You do this exactly the same as in the question you contacted. There is no such thing as a β€œdynamic” table. The table simply displays your model. Your model may be dynamic, but not in the table. In any case, follow the answers to this question and you will have a switch in your table view.

0
source

Source: https://habr.com/ru/post/916905/


All Articles