2012-08-14 49 views
0

我有一个带有自定义单元TaskCell的UITableView。 TaskCell有checkboxImageView,我希望当用户点击checkboxImageView时会激发一个方法。出于某种原因,它总是触发didSelectTableView委托方法。这里是我的代码:在UITableView中为UIImageView调用手势识别器

- (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; 
} 
+0

如果我在cellForIndex中附加手势识别器,那么它工作正常。 – azamsharp 2012-08-14 21:22:43

回答

0

如果您在TaskCell.m增加了手势识别,然后将下面的行

singleTapGestureRecognizer.delegate = self; 

设置识别到小区的delegate,不是你的控制器。您应该在您的控制器文件中添加识别器,以便正确设置delegate

我不确定这是否会起作用,因为我还没有尝试过自己,但是您可以尝试在Storyboard中设置识别器,并让它成为您的手机的@property。然后,您可以将委托设置为控制器文件中的控制器。