我有一个带有自定义单元 TaskCell 的 UITableView。TaskCell 具有 checkboxImageView ,我希望当用户单击 checkboxImageView 时会触发一个方法。由于某种原因,它总是触发 didSelectTableView 委托方法。这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TaskCell";
Task *task = [tasks objectAtIndex:[indexPath row]];
TaskCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TaskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell bindTo:task];
return cell;
}
TaskCell.m:
-(void) prepareGestureRecognizers
{
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSingleTap:)];
singleTapGestureRecognizer.delegate = self;
singleTapGestureRecognizer.numberOfTapsRequired = 1;
self.checkboxImageView.userInteractionEnabled = YES;
[self.checkboxImageView addGestureRecognizer:singleTapGestureRecognizer];
}
-(void) onSingleTap:(UITapGestureRecognizer *) sender
{
NSLog(@"single tap!");
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) bindTo:(Task *)task
{
[self prepareGestureRecognizers];
self.titleLabel.text = task.title;
}