iphone - 如何检测用户是否直接单击 cell.imageView 而不是 tableview 单元格中的标签?

标签 iphone objective-c ios

在我的 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);
}

关于iphone - 如何检测用户是否直接单击 cell.imageView 而不是 tableview 单元格中的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6216390/

相关文章:

iphone - 当应用程序处于后台时,每 X 秒获取一次用户坐标

iphone - 方向更改时 UICollectionView 中的分页问题

objective-c - 如何让 iPhone 的 FMOD 声音在 iPhone 锁定时不暂停?

iPhone 数据表单提交最佳实践?

ios - QuickBlox 聊天 : can't perform video call

ios - 以编程方式对齐 iPhone 键盘顶部的工具栏

ios - 如何清除 iPhone/iPad 上 iOS 应用程序的数据

objective-c - 使用 NSURLSessionDataTask 进行线程处理

ios - 展开/最小化 UITableView 自定义单元格

ios - 没有断点就无法播放音频