2013-10-22 50 views
-1

我正在尝试制作自定义单元格,因为它发现设备时生成了原型单元格,这意味着每个设备都有一行。每个按钮都必须在自己的设备上运行。定制单元格:enter image description here每个单元格的单独按钮,创建为设备时创建的

我创建了一个UITableViewCell并声明了标签,按钮和图像。我无法使按钮充当在TableViewController.Here各个按钮是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中细胞声明:

- (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]; 
    } 
} 

我想我的问题是面向对象,我宣称它是正确的?我应该在哪里做这个? 谢谢。

回答

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];