我有一个自定义单元格,它是UITableViewCell的子类。我在自定义单元格中添加了按钮的单击事件,但单击按钮后没有任何反应。
我的代码基本上如下:
MyCustomCell.h:
@interface MyCustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *thumbnailButton;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
// bla bla...
@end
MyTableViewController.h:
@interface MyTableViewController : UITableViewController
// bla bla...
- (void)thumbnailButtonClicked:(UIButton *)sender;
// bla bla...
@end
MyTableViewController.m:
#import "MyTableViewController.h"
#import "MyCustomCell.h"
// bla bla...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"MyCustomCell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell.contentView setUserInteractionEnabled:NO];
[cell.thumbnailButton addTarget:self
action:@selector(thumbnailButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
cell.thumbnailButton.tag = indexPath.row;
return cell;
}
- (void)thumbnailButtonClicked:(UIButton *)sender
{
NSLog(@"[%d] Button Clicked!", sender.tag);
}
上面的thumbnailButton的协调是(7,6,100,66),它不超过其父框架。
有人可以告诉我如何解决这个问题吗?我已经挣扎了一段时间......