在我的tableView小區i有:如何檢測用戶是否直接點擊cell.imageView而不是在tableview單元格中的標籤上?
cell.imageView.image = image;
我怎麼能直接檢測到圖像上的用戶點擊,而不是cell.textLabel?
在我的tableView小區i有:如何檢測用戶是否直接點擊cell.imageView而不是在tableview單元格中的標籤上?
cell.imageView.image = image;
我怎麼能直接檢測到圖像上的用戶點擊,而不是cell.textLabel?
添加一個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);
}
很酷的解決方案:)。奇蹟般有效。 – joey 2011-06-02 17:13:24
正如一些評論指出的那樣,您應該改用UIButton
。 A UIImageView
不適用於處理用戶交互操作,只需創建按鈕,添加目標並將其添加爲單元格的子視圖就簡單多了。
改用UIButton .. – Jhaliya 2011-06-02 15:13:22
'UIButton'正常工作! – 2011-06-02 15:18:54