ios - 区分accessoryView和Row on touch

标签 ios objective-c accessory

我希望用户在单击表格单元格而不是行中的附件图像时获得不同的结果。

为了捕获他们对配件的点击,我使用以下方法:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;{
NSLog(@"accessory button tapped");
  }

此外,我没有使用标准指标。相反,我使用自定义图像,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 [cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark.png"]]];

}

覆盖标准附件按钮并显示图像。注意:无论 Storyboard中的公开指示器设置为“无”、复选标记、公开指示器等,它都会显示图像。

但是,目前

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;{
NSLog(@"accessory button tapped");
  }

方法未触发。

有一些answers SO 表明,如果附件是附件 View 而不是附件类型,则不会调用此方法。

有谁知道是否可以使用自定义图像并且仍然可以通过点击按钮的方法来触发?

最佳答案

您可以通过以下方式在 UIImageView 上添加标签手势

UIImageView *imgView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark.png"]];
imgView.tag = indexPath.row;
UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:)];
tapGesture1.numberOfTapsRequired = 1;
[tapGesture1 setDelegate:self];
[imgView addGestureRecognizer:tapGesture1];
[cell setAccessoryView:];

并添加选择器方法

- (void) tapGesture: (id)sender
{
    //handle Tap...
}    

希望对你有帮助

关于ios - 区分accessoryView和Row on touch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991284/

相关文章:

ios - 如何清除 Objective-C 中的所有 NSUserDefaults 值?

ios - 在 iOS 中读取 URL 的缓存问题

iOS 上的 C++ 原子

ios - 需要帮助设置后台托管对象上下文。在 FRC controllerDidChangeContent 上获取异常

ios - 来自 CMSampleBufferRef 转换的 UIImage,导致 UIImage 无法正确呈现

objective-c - didSelectRowAtIndexPath : how to get old indexPath to deselect checkmark?

iphone - 当外部配件通过 BT 配对或插入底座连接器时如何启动 iPhone 应用程序

ios - UITableViewCell 标签不需要的缩进

android - Samsung Galaxy S4 未检测到 USB 串行 FT232R

安卓 USB 配件