2012-09-24 20 views
5

Dodaję przycisk w komórce widoku tabeli dynamicznie. Przycisk pokazany na stole, ale nie mogę ich kliknąć. Tu jest mój kod,Jak dodać klikalny przycisk na komórce tabeli?

Kod moja klasa 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; 
} 

Zarzut se pomóż mi. Dzięki.

+0

Czy ten przycisk na każdym widoku? Czy jest prawidłowo renderowany? – ThomasW

+0

Ya jest warunek, jeśli warunek jest prawdziwy, wtedy przycisk pojawi się na widoku. Przycisk jest wyświetlany prawidłowo, ale nie mogę wywołać metody kliknięcia. – Saurabh

+0

Powinieneś również pokazać kod, którego używasz do tworzenia komórek tabeli. Co się dzieje, gdy klikniesz komórkę przycisku? Czy jest awaria? Czy w ogóle nie ma efektu? – ThomasW

Odpowiedz

10

proponuję usuwasz swój outlet Button w swoim TableViewCell. i po prostu stwórz swój przycisk dynamicznie w cellForRowAtIndexPath: Stworzyłem klasę SampleCell, podklasę UITableViewCell, ma ona gniazdo UILabel o nazwie "lbl". To powinno działać na twoim kodzie, zakładając, że twój selektor jest w tej samej klasie, w której umieszczasz 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

Dziękuję za odpowiedź. Ale wciąż nie działa. :( – Saurabh

+0

hmm dziwne .. Zakodowałem to jakiś czas temu. Czy usunąłeś swój UIButton IBOutlet * btn w swojej klasie TableViewCell? –

+0

Użyj cell.bounds.origin, a nie cell.frame.origin dla ramki przycisku. –

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

Przepraszam, to mój błąd, gdy zamieszczam pytanie. Ale nie działa ... a linia kodu, którą podałeś, również nie działa. To jest moja metoda, którą nazwałem - (IBAction) show_chat_pic: (id) nadawca { NSLog (@ "w show"); UIImageView * imageview = [[przydział UIImageView] initWithFrame: CGRectMake (5, 50, 310, 320)]; imageview.image = sender_profile2; [self.view addSubview: imageview]; } – Saurabh

+2

Brak odpowiedzi .... To powinien być komentarz ... – Maulik

+0

chk zaktualizowana odpowiedź @ użytkownik1652551 – Rajneesh071

0

nie dały żadnego zdarzenia sterowania przycisku lub zmienić ją na UIControlEventTouchUpInside z UIControlStateNormal i zrobić to tylko raz :)

+0

Myślę, że podałem wydarzenie [cell.chat_pic_btn addTarget: self action: @selector (show_chat_pic :) forControlEvents: UIControlEventTouchUpInside]; – Saurabh

+0

Sprawdź ten link i spróbuj zmodyfikować swój kod http: // stackoverflow.com/questions/10732229/uibutton-nie-odpowiadające-kliknięcie-zdarzenie-in-uitableviewcell – IronManGill

5

należy ustawić UserInteraction YES widzenia komórki a przycisk

self.userInteractionEnabled = YES; 
Powiązane problemy