【问题标题】:Error when handling touch on UIImageView in a cell?处理单元格中的 UIImageView 触摸时出错?
【发布时间】:2012-01-07 14:41:50
【问题描述】:

我不知道为什么会这样,但我得到了这个错误:

-[__NSArrayM 部分]:无法识别的选择器发送到实例 0x7e53b70 2012-01-07 15:35:44.108 Timely1[51661:15203] * 终止 应用程序由于未捕获的异常“NSInvalidArgumentException”,原因: '-[__NSArrayM section]: 无法识别的选择器发送到实例 0x7e53b70'

handleTouch 被激活时。这是我添加图像和点击手势的代码。

[cell.imageView setUserInteractionEnabled:YES];
[cell.imageView setImage:[UIImage imageNamed:@"checkbox.PNG"]];

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouch:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];   
[tapped release];

然后是我处理触摸的代码:

-(void)handleTouch:(UITapGestureRecognizer *)gesture
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[array count] inSection:1];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    [cell.imageView setImage:[UIImage imageNamed:@"checkbox_checked.PNG"]];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:array] withRowAnimation:UITableViewRowAnimationFade];
}

更新:如果我想取消选中它(比如切换它),有人知道怎么做吗?

【问题讨论】:

  • 在我看来,您之前遇到的问题在于 - NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[array count] inSection:1];相反,您需要使用 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[array count]-1 inSection:1];
  • 是的,但你知道如何修复[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:array] withRowAnimation:UITableViewRowAnimationFade];,因为我发现这是问题所在,但如果我想再次重新加载一个单元格,我就不能再使用该行了。
  • 你是否在数组中传递 NSIndexPath 的对象?
  • 这就是我声明我的数组的方式:array = [NSMutableArray arrayWithArray:db.dbArray]; 并且 dbArray 是一个包含我所有保存数据的数组。
  • dbarray 中存在什么?您打印了吗?它们是 nsindexpath 的一种吗?

标签: iphone uitableview touch uitapgesturerecognizer


【解决方案1】:

在您的 handleTouch 方法中,我认为您可以使用:

UIImageView *cellImageView=(UIImageView *)gesture.view;

【讨论】:

  • 感谢工作。问题是[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:array] withRowAnimation:UITableViewRowAnimationFade];,这就是错误的原因。
  • 如果我想取消选中它(比如切换它)你知道怎么做吗?
  • 这让我开始了,但我需要更多的数据,而不是我的图像状态,所以我决定创建一个类别扩展 @interface UITapGestureRecognizer (Utility),添加一个名为 userData 的附加属性 @property (nonatomic, strong) id userData; 来放置和稍后检索我需要的参数。
【解决方案2】:

我决定使用UIButton 作为我的复选框并继承我的UITableViewCell,这似乎更容易,更合乎逻辑,尤其是使用切换位。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多