2011-06-02 62 views

回答

5

添加一个UITapGestureRecognizer到imageView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    /* ... */ 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.imageView.image = /*...*/ 

     UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)] autorelease]; 
     [cell.imageView addGestureRecognizer:tapGesture]; 
     cell.imageView.userInteractionEnabled = YES; 
    } 
    /* ... */ 
} 

- (void)imageTapped:(UITapGestureRecognizer *)gesture { 
    UITableViewCell *cell = [[[gesture view] superview] superview]; 
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForCell:cell]; 
    NSLog(@"Image Tap %@", tappedIndexPath); 
} 
+1

很酷的解决方案:)。奇迹般有效。 – joey 2011-06-02 17:13:24

1

正如一些评论指出的那样,您应该改用UIButton。 A UIImageView不适用于处理用户交互操作,只需创建按钮,添加目标并将其添加为单元格的子视图就简单多了。

相关问题