【问题标题】:UIimageView and Acessory View to work separately in TableViewCellUIimageView 和 Accessory View 在 UITableViewCell 中分别工作
【发布时间】:2013-02-06 10:09:10
【问题描述】:

我正在开发一款类似于 Apple 提醒应用的应用。我有一个带有数据的 tableView。该单元格具有 UIImageView 和 Disclosure Indicator 附件视图。当它的任何一行被选中时,就会推送一个视图控制器。

我希望每行的 UIImageView 与行选择分开工作,就像在 Apple 提醒应用程序中一样。我怎样才能做到这一点?

我已尝试将 UIimageview 作为单元格的子视图,并将 UITapGestureRecognizer 添加到其中。它适用于最后插入的行,但不适用于其他行。

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    您可以考虑子类化 UITableViewCell,而不是子类 UIImageView。在您的子类中,您仍然可以将 UITapGestureRecognizer 添加到 UIImageView,但您将针对您的单元格子类的方法(而不是您的表格控制器,尽管您没有说明处理程序当前的目标是什么)。

    为您的自定义单元格的 UIImageView 添加一个 tapRecognizer,并在您的自定义单元格上添加一个 BOOL 标志(例如,imageViewTapped)。在您的点击处理程序中,将 imageViewTapped 设置为 YES。

    现在,无论点击是否在 imageVIew 上,您的 tableView 控制器仍将调用 didSelectRowWithIndexPath。因此,在 didSelectRowAtIndexPath 中,评估自定义单元格的 imageViewTapped 属性。如果已设置标志,则将 viewController A(用于 imageView 行为)推送到导航堆栈上。如果 imageViewTapped == NO,推送 viewController B(大概是你已经过渡到的)。

    【讨论】:

      【解决方案2】:

      @阿德尔。您可以在 imageView 上放置一个透明按钮以响应用户的触摸。

      CGRect imageRect = (CGRect){100, 100, 100, 100};
      UIImageView *imageView = [[[UIImageView alloc] initWithFrame:imageRect] autorelease];
      imageView.backgroundColor = [UIColor yellowColor];
      [self.view addSubview:imageView];
      imageView.userInteractionEnabled = YES;
      
      UIButton *maskBtn = [UIButton buttonWithType:UIButtonTypeCustom];
      maskBtn.frame = imageView.bounds;
      maskBtn.backgroundColor = [UIColor clearColor];
      [maskBtn addTarget:self action:@selector(maskBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
      [imageView addSubview:maskBtn];
      

      注意 imageView 的 userInteractionEnabled 默认为 NO。 希望有所帮助。 :)

      【讨论】:

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