我正在尝试创建一个自定义单元格,因为原型单元格在找到设备时生成,这意味着每个设备都有一行。每个按钮必须在自己的设备上操作。自定义单元格:
我创建了一个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,我声明它是正确的?我应该在哪里这样做? 感谢。
答案 0 :(得分: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];