在Tableview上添加开关按钮并禁用单元格选择

时间:2013-03-01 11:34:02

标签: iphone ios objective-c xcode cocoa-touch

我在单元格上添加了切换按钮但同时我希望我的单元格不应该在选择时突出显示或禁用选择。我确实禁用了用户交互属性,但现在我也无法更改单元格的位置。这是我在Cell上添加Switch按钮所做的。

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

现在单元格没有选择,但我也无法改变开关的位置。提前致谢。欢呼声。

3 个答案:

答案 0 :(得分:3)

cell.userInteractionEnabled=NO;

请勿使用此代替以下内容:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

此行将帮助您切换到工作状态,但不会选择单元格。

答案 1 :(得分:1)

您可以通过将单元格选择样式设置为无来禁用单元格选择,如@Ann所述。

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" );
}

答案 2 :(得分:0)

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

//cell.userInteractionEnabled=NO;

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

将UISwitch添加到cell.contentView