ios - UISwitch 在 UITableview(iOS 8)中不可见?

标签 ios uitableview uiswitch

我在 UITableview 中创建了一个 UISwitch,但它在确切的行中不可见。我在项目中引用和实现的代码。谁能告诉我如何更正它。

if (indexPath.row == 1) {

 UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(200, 10, 150, 50)];
 [switchBtn setOn:YES animated:YES];
 NSInteger row;
 switchBtn.tag=row;
 row =[indexPath row];
 [switchBtn addTarget:self action:@selector(switchStateChanged:) forControlEvents:UIControlEventValueChanged];
 cell.accessoryView = switchBtn;
 [cell addSubview:switchBtn];

 UILabel *lblPhone =[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 230, 30)];
 lblPhone.text = @"Change Default View as Month View";
 lblPhone.font = [UIFont systemFontOfSize:12];
 [cell.contentView addSubview:lblPhone];

}
-(IBAction)switchStateChanged:(id)sender
{
UISwitch *switchState = sender;

if(switchState.tag == 0)
{
     NSLog(@"ON");
}
else {
    NSLog(@"OFF");

    }
 }

enter image description here

最佳答案

引用这段代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
if (indexPath.row == 1) {

    UISwitch *switchBtn = [[UISwitch alloc] init];
    [switchBtn setOn:YES animated:NO];
    switchBtn.tag=[indexPath row];
    [switchBtn addTarget:self action:@selector(switchStateChanged:) forControlEvents:UIControlEventValueChanged];
    cell.accessoryView = switchBtn;
    UILabel *lblPhone =[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 230, 30)];
    lblPhone.text = @"Change Default View as Month View";
    lblPhone.font = [UIFont systemFontOfSize:12];
    [cell.contentView addSubview:lblPhone];

}
// Configure the cell...

return cell;
}
-(void)switchStateChanged:(id)sender
{
UISwitch *switchControl = sender;

if(switchControl.tag == 1)
{
    if (switchControl.isOn) {
        NSLog(@"on");
    }else{
        NSLog(@"off");
    }
}
}

屏幕截图

enter image description here

关于ios - UISwitch 在 UITableview(iOS 8)中不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30366485/

相关文章:

ios - 如何阻止 TableViewCell 重复图像

ios - tableViewCell 按钮上的线程 1 fatal error

iOS7 UISwitch 其事件 ValueChanged : Calling continuously is this Bug or what. .?

ios - 等效于 UISwtich 中的 UISegmentedControl selectedSegmentIndex - Objective C

uiswitch - "invalid mode ' kCFRunLoopCommonModes' ...”是什么意思?

swift - 如何更改 UITableViewCellAccessoryType.Checkmark 的颜色?

ios - 在 Swift 2.0 中向 UITableView 添加图像

iphone - Localizable.strings 未能加载所有其他构建?

ios - 快速使用外部 key 加密数据

ios - 尝试在 iOS 中分发 Flutter 应用时出现错误 "Invalid Provisioning Profile Signature"