2011-06-02 61 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不適用於處理用戶交互操作,只需創建按鈕,添加目標並將其添加爲單元格的子視圖就簡單多了。

相關問題