-1

我正在尝试制作一个自定义单元格,在找到设备时生成原型单元格,这意味着每个设备都有一行。每个按钮都必须在自己的设备上起作用。自定义单元格:在此处输入图像描述

我创建了一个 UITableViewCell 并声明了标签、按钮和图像。我无法让按钮在 TableViewController 上充当单独的按钮。这是 TableViewController.m

- (interruptorTableCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifier = @"dimmableCell";
interruptorTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
BinaryLight1Device *dispositivoApresentadoNaTabRetiradoDaListaDeFiltrados = [self.model.devicesFiltrados objectAtIndex:indexPath.row];
cell.accessoryView = cell.ligaDeslBtnPro;
cell.ligaDeslBtnPro.tag = indexPath.row;
[cell.ligaDeslBtnPro addTarget:self action:@selector(ligaDeslBtn:) forControlEvents:UIControlEventTouchUpInside];
cell.dimmableLabel.text  = [dispositivoApresentadoNaTabRetiradoDaListaDeFiltrados friendlyName];
return cell;
}

这是在 TableView Cell 中声明的按钮操作:

- (IBAction)ligaDeslBtn:(UIButton *)sender
{
interruptorTableCell *cell = (interruptorTableCell*)[sender superview];
NSIndexPath *index=[menuView indexPathForCell:cell];
BinaryLight1Device *deviceBinaryLight = [model.devicesFiltrados objectAtIndex:sender.tag];
NSMutableString *statusDispositivos = [[NSMutableString alloc]init];
if (sender)
{
    if ([statusDispositivos isEqualToString:@"0"])
    {
     [[deviceBinaryLight switchPower]SetarValorLigadoDesligado:@"1"];
     [[deviceBinaryLight switchPower]RetornarValorLigadoDesligado:statusDispositivos];
    }
    else if ([statusDispositivos isEqualToString:@"1"])
    {
        NSLog(@"Desligando dispositivo");
        [[deviceBinaryLight switchPower]SetarValorLigadoDesligado:@"0"];
        [[deviceBinaryLight switchPower]RetornarValorLigadoDesligado:statusDispositivos];
     }
}

我认为我的问题在于 OO,我声明它正确吗?我应该在哪里做这个?谢谢。

4

1 回答 1

2

我尝试过一次并为我工作的一种方法是为每个单元格添加一个按钮并使用标签:

将此添加到

- (interruptorTableCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}


// Configure your buttons according your needs
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(530.0f, 20.0f, 87.0f, 34.0f);
[button addTarget:self action:@selector(ligaDeslBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:button];
    button.tag = indexPath.row;

并且,在您的按钮事件中,在开头添加://标识被点击的按钮。

NSInteger row = [sender tag];  
于 2013-10-22T13:56:45.523 回答