josh
josh

Reputation: 1689

Add switch button on Tableview and disable cell selection

I added switch button on cell but meanwhile i want that my cell shouldn't be either highlighted on selection or disable selection. I did disabled user interaction property but now I can't change position of cell also. Here is what I did to add Switch button on Cell.

UISwitch* switchFB = [[UISwitch alloc] initWithFrame:CGRectMake(227, 8, 79, 27)];
cell.textLabel.text=@"Facebook";
        cell.userInteractionEnabled=NO;
        cell.accessoryView = switchFB;

Now cell is not selecting but i can't change position of switch either too. Thanks in advance. Cheers.

Upvotes: 1

Views: 2449

Answers (3)

Kasaname
Kasaname

Reputation: 1501

cell.userInteractionEnabled=NO;

Don't use this instead of use below :

cell.selectionStyle = UITableViewCellSelectionStyleNone;

This line will help ur switch to work but cell won't get selected.

Upvotes: 3

Pratik Mistry
Pratik Mistry

Reputation: 2945

You can disable the cell selection by setting cell selection style to none like @Ann said.

cell.selectionStyle = UITableViewCellSelectionStyleNone;

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = switchView;
[switchView setOn:YES animated:NO];
[switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

- (void) switchChanged:(id)sender {
    UISwitch* switchControl = sender;
    NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );
}

Upvotes: 2

iPatel
iPatel

Reputation: 47119

UISwitch* switchFB = [[UISwitch alloc] initWithFrame:CGRectMake(227, 8, 79, 27)];
cell.textLabel.text=@"Facebook";

//cell.userInteractionEnabled=NO;

[cell.contentView addSubview: switchFB ];
cell.selectionStyle= UITableViewCellSelectionStyleNone;

Add UISwitch to cell.contentView

Upvotes: 0

Related Questions