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.
Czy ten przycisk na każdym widoku? Czy jest prawidłowo renderowany? – ThomasW
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
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