2012-09-24 16 views
5

Aggiungo dinamicamente un pulsante nella cella di visualizzazione tabella. Pulsante mostrato sul tavolo ma non riesco a selezionarli. Ecco il mio codice,Come aggiungere un pulsante cliccabile sulla cella della tabella?

Questo codice mia classe tableviewcell:

MessageTableViewCell.h

#import <UIKit/UIKit.h> 
@interface MessageTableViewCell : UITableViewCell { 
{IBOutlet UIButton *chat_pic_btn;}} 
@property (nonatomic, retain) UIButton *chat_pic_btn; 
@end; 

MessageTableViewCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {chat_pic_btn=[[UIButton alloc]init]; 
[self.contentView addSubview:chat_pic_btn];}} 

MessageTable.m

-(void)customActionPressed :(id)sender 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Custom Button Pressed" 
                 message:[NSString stringWithFormat: @"You pressed the custom button on cell"] 
                 delegate:self cancelButtonTitle:@"Great" 
               otherButtonTitles:nil]; 
    [alertView show]; 
} 

- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"CellIdentifier"; 
    MessageTableViewCell *cell = (MessageTableViewCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     // cell = [[MessageTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]; 
     cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
cell.chat_pic_btn.frame = CGRectMake(180, 24, 70,35); 
      [cell.chat_pic_btn setImage:[UIImage imageNamed:@"done.png"]forState:UIControlStateNormal]; 
      [cell.chat_pic_btn addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchDown]; 
return cell; 
} 

Plea se mi aiuti. Grazie.

+0

È questo tasto su ogni punto di vista? È reso correttamente? – ThomasW

+0

Ya esiste una condizione se la condizione è vera, quindi il pulsante verrà visualizzato nella vista. Il pulsante viene visualizzato correttamente, ma non sono in grado di chiamare un metodo al clic. – Saurabh

+0

Probabilmente dovresti anche mostrare il codice che usi per creare le celle della tabella. Inoltre, cosa succede quando si fa clic sulla cella pulsante? C'è un incidente? O nessun effetto? – ThomasW

risposta

10

Suggerisco elimini la presa Button in TableViewCell. e basta creare il tuo pulsante dinamicamente in cellForRowAtIndexPath: Ho creato una classe SampleCell, sottoclasse di UITableViewCell, ha uno sbocco UILabel che ho chiamato "lbl". Questo dovrebbe funzionare sul tuo codice, assumendo che il tuo selettore si trovi sulla stessa classe in cui inserisci la tua tablview;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"SampleCell"; 
    SampleCell *cell = (SampleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SampleCell" owner:self options:nil]; 
     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
       cell = (SampleCell *)currentObject; 
       break; 
      } 
     } 
    } 
    // Configure the cell. 
    cell.lbl.text = @"Hello"; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    //set the position of the button 
    button.frame = CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y + 20, 100, 30); 
    [button setTitle:@"World" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    button.backgroundColor= [UIColor clearColor]; 
    [cell.contentView addSubview:button]; 

    return cell; 
} 
+2

Grazie per la risposta. Ma ancora non funziona. :( – Saurabh

+0

hmm strano .. L'ho codificato qualche tempo fa .. hai cancellato IBOutlet UIButton * btn nella tua classe TableViewCell? –

+0

Usa cell.bounds.origin, non cell.frame.origin per il frame del pulsante. –

1
UIButton *yourBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0, 50, 50)]; 
[yourBtn setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal]; 
[yourBtn addTarget:self action:@selector(submitBtnPress:) forControlEvents:UIControlEventTouchUpInside]; 

cell.accessoryView = yourBtn; 
+0

scusa, è un mio errore quando invio una domanda. Ma non funziona ... e anche la linea di codice che hai fornito non funziona. Questo è il mio metodo che ho chiamato - (IBAction) show_chat_pic: (id) mittente { NSLog (@ "in show"); UIImageView * imageview = [[UIImageView alloc] initWithFrame: CGRectMake (5, 50, 310, 320)]; imageview.image = sender_profile2; [self.view addSubview: imageview]; } – Saurabh

+2

Non è una risposta .... Va commento ... – Maulik

+0

chk risposta aggiornata @ user1652551 – Rajneesh071

0

Non hai dato ogni caso il controllo al pulsante o modificarlo per UIControlEventTouchUpInside da UIControlStateNormal e di farlo solo una volta :)

+0

credo di avere dato l'evento [cell.chat_pic_btn addTarget: azione di auto: @selector (show_chat_pic :) forControlEvents: UIControlEventTouchUpInside]; – Saurabh

+0

Controlla questo link e prova a modificare il codice http: // stackoverflow.it/questions/10732229/uibutton-not-responding-to-click-event-in-uitableviewcell – IronManGill

5

assicurarsi di impostare UserInteraction su SÌ in vista di cella e il pulsante

self.userInteractionEnabled = YES; 
Problemi correlati