我在表格视图单元格中动态添加一个按钮。按钮显示在桌面上,但我无法点击它们。这里是我的代码,如何在表格单元上添加可点击的按钮?
这是我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;
}
认罪我帮助我。 谢谢。
是每次鉴于此按钮?它是否正确渲染? – ThomasW
如果条件为真,那么Ya有条件,那么按钮将显示在视图上。按钮显示正确,但我无法通过点击调用方法。 – Saurabh
您应该也可以显示您用于创建表格单元格的代码。另外,当你点击按钮单元格时会发生什么?有没有崩溃?或者根本没有效果? – ThomasW