【问题标题】:UISwitch in table view were not able to disabled by a custom switch表视图中的 UISwitch 无法通过自定义开关禁用
【发布时间】:2015-12-08 10:56:30
【问题描述】:

我打算使用自定义开关来控制在 tableview 中以编程方式设计的所有开关。我尝试使用自定义Switch的控制事件来控制所有的开关,但还是不行。我已经在 VC 的实现中为 tableview 声明了 UISwitch。

@implementation ViewController
{
    UISwitch *switchObj;
    NSArray *infoDetail;
}
@synthesize customSwitch;
- (void)viewDidLoad {
    [super viewDidLoad];
    infoDetail = [NSArray arrayWithObjects:@"Main door",@"Main door 2",@"Main windows",nil];
    [customSwitch addTarget:self action:@selector(stateChanged:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [infoDetail objectAtIndex:indexPath.row];  
switchObj = [[UISwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 20.0)];
switchObj.transform = CGAffineTransformMakeScale(1, 1);
[switchObj addTarget:self action:@selector(alarmOn:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
cell.accessoryView = switchObj;
return cell;
}
- (void)stateChanged:(UISwitch *)switchState{
    if (customSwitch.isOn) {
     switchObj.enabled = YES;
    }
    else{
    switchObj.enabled = NO;
    }
}
- (void)alarmOn:(id)sender{
if ([sender isOn]) {
    NSLog(@"Switch toggled on");
}else{
    NSLog(@"Switch toggled off");
    }
}

【问题讨论】:

  • 你遇到了什么问题。此外,您只添加了开关,还没有提供任何类型的标签或其他东西来处理它们。

标签: objective-c uitableview uiswitch


【解决方案1】:

尝试将您的 switchObj 声明代码放入 UITableView 的 cellForRowAtIndex 方法并从顶部删除。

UISwitch *switchObj;

现在方法是这样的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [infoDetail objectAtIndex:indexPath.row];  
    UISwitch *switchObj = [[UISwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 20.0)];
    switchObj.transform = CGAffineTransformMakeScale(1, 1);
    [switchObj addTarget:self action:@selector(alarmOn:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
    cell.accessoryView = switchObj;

    if (customSwitch.isOn) {
        switchObj.enabled = YES;
    }
    else{
        switchObj.enabled = NO;
    }
    return cell;
}

另外,当您更改状态自定义开关时,也要重新加载 tableView。
更改您的 stateChanged 方法如下:

-(void)stateChanged:(UISwitch *)switchState{
    [myTableView reloadData];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2012-05-24
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多