2013-10-22 80 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];