【问题标题】:Detect Tap on UIImageView within UITableViewCell在 UITableViewCell 中检测 UIImageView 上的点击
【发布时间】:2013-11-14 17:35:11
【问题描述】:

我有一个自定义的复杂 UITableViewCell,其中有很多视图。我在其中有一个 UIImageView,它在特定条件下可见。当它可见时,

  1. 当用户点击 UIImageView 时,我必须执行一些操作。

  2. 我知道我必须为此任务触发选择器。但我也想将一个值传递给该方法(请参阅下面的 -(void)onTapContactAdd :(id) sender : (NSString*) uid),这将被称为我在 UITableViewCell 中的 UIImageView 上的点击动作我正在谈论.这是因为,使用传递的值,被调用的方法将完成它的工作。

这是我迄今为止尝试过的。

cell.AddContactImage.hidden = NO ;
cell.imageView.userInteractionEnabled = YES;

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapContactAdd::)];
[tap setNumberOfTouchesRequired:1];
[tap setNumberOfTapsRequired:1];
[tap setDelegate:self];
[cell.AddContactImage addGestureRecognizer:tap];



-(void)onTapContactAdd :(id) sender : (NSString*) uid
{
    NSLog(@"Tapped");
// Do something with uid from parameter
}

点击时不会调用此方法。我已经在头文件中添加了。

提前感谢您的帮助。

【问题讨论】:

  • 手势识别器的动作方法只能有一个参数,即“sender”。你从哪里得到你想要传递的uid?
  • 如果我调用该方法并正确获取该值,那么我没有理由问这个问题:-)。上面的代码是我迄今为止尝试过的,我知道它不正确,但这就是我想要做的。

标签: ios objective-c uitableview uiimageview uigesturerecognizer


【解决方案1】:

也许不是理想的解决方案,但为每个 UIImageViews 添加标签。然后有一个 NSArray,其 uid 对应于标签值

所以在你的代码中某处创建数组

NSArray *testArray = [NSArray arrayWithObjects:@"uid1", @"uid2", @"uid3", @"uid4", @"uid5", @"uid6", nil];

然后,当您设置表格视图单元格时,将标签设置为行#

//Set the tag of the imageview to be equal to the row number 
cell.imageView.tag = indexPath.row;

//Sets up taprecognizer for each imageview
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                      action:@selector(handleTap:)];
[cell.imageView addGestureRecognizer:tap];

//Enable the image to be clicked 
cell.imageView.userInteractionEnabled = YES;

然后在被调用的方法中就可以得到这样的标签

- (void)handleTap:(UITapGestureRecognizer *)recognizer  
{    
     NSString *uid = testArray[recognizer.view.tag];    
}

【讨论】:

    【解决方案2】:

    将手势识别器添加到单元格本身。

    然后在动作选择器中,执行以下操作以了解哪个视图被点击:

    -(IBAction)cellTapped:(UITapGestureRecognizer*)tap
    {
        MyCustomTableViewCell* cell = tap.view;
        CGPoint point = [tap locationInView:cell.contentView];
        UIView* tappedView = [cell.contentView hitTest:point withEvent:NULL];
    
        if (tappedView==cell.myImageView) {
            // Do whatever you want here,
        }
        else { } // maybe you have to handle some other views here
    }
    

    【讨论】:

      【解决方案3】:

      手势识别器只会将一个参数传递给动作选择器:它自己。所以你需要单独传递uid值。像这样。

      猜测这在 cellForRowAtIndexPath: 方法中

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          //code
          cell.AddContactImage.hidden = NO ;
          cell.imageView.userInteractionEnabled = YES;
          cell_Index=indexPath.row ;
          UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self               action:@selector(onTapContactAdd:)];   //just one arguement passed
          [tap setNumberOfTouchesRequired:1];
          [tap setNumberOfTapsRequired:1];
          [tap setDelegate:self];
          [cell.AddContactImage addGestureRecognizer:tap];
          //rest of code
      }
      
      -(void)onTapContactAdd :(NSString*) uid
      {
           NSLog(@"Tapped");
           CustomCell *cell=(CustomCell *)[yourtableView cellForRowAtIndexPath:[NSIndexPath  indexPathForRow:cell_Index inSection:0]]; 
           //cell.AddContactImage will give you the respective image .
           // Do something with uid from parameter .
      }
      

      因此,当您点击相应自定义单元格中的可见图像时,onTapContactAdd: 方法会使用相应的 uid 值(参数)调用,现在我们有了 cell.AddContactImage 也可以访问,我相信这就是您尝试的原因将它与参数一起传递。 希望对你有帮助!!!

      【讨论】:

        【解决方案4】:

        您可以使用ALActionBlocks 向 UIImageView 添加手势并在块中处理操作

        __weak ALViewController *wSelf = self;
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) {
            NSLog(@"pan %@", NSStringFromCGPoint([weakGR locationInView:wSelf.view]));
        }];
        [self.imageView addGestureRecognizer:gr];
        

        安装

        pod 'ALActionBlocks'
        

        【讨论】:

          【解决方案5】:

          另一个,indexPath,如果您可以处理 DataSource 中的点击:

          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseId)! as! ...ListCell
              ...
              cell.theImage.isUserInteractionEnabled = true
              let onTap = UITapGestureRecognizer(target: self, action: #selector(onTapImage))
              onTap.numberOfTouchesRequired = 1
              onTap.numberOfTapsRequired = 1
              cell.theImage.addGestureRecognizer(onTap)
              ...
              return cell
          }
          
          func onTapImage(_ sender: UITapGestureRecognizer) {
              var cell: ...ListCell?
              var tableView: UITableView?
              var view = sender.view
              while view != nil {
                  if view is ...ListCell {
                      cell = view as? ...ListCell
                  }
                  if view is UITableView {
                      tableView = view as? UITableView
                  }
                  view = view?.superview
              }
          
              if let indexPath = (cell != nil) ? tableView?.indexPath(for: cell!) : nil {
                  // this is it
              }
          }
          

          如果您只有一个tableView,您可能希望缩短代码。

          【讨论】:

            【解决方案6】:

            在这里,我们有 customtableviewcell .h 和 .m 文件,单元格中有两个图像。和 HomeController,它们有 tableview 来访问这个单元格。如所述,这是检测 UIImage 上的 Tap。

                        **CustomTableViewCell.h**
            
                        @protocol CustomTableViewCellDelegate <NSObject>
            
                        - (void)didTapFirstImageAtIndex:(NSInteger)index;
                        -(void)didTapSettingsImagAtIndex:(NSInteger)index;
            
                        @end
            
            
                        @interface CustomTableViewCell : UITableViewCell
                        {
            
                            UITapGestureRecognizer *tapGesture;
                            UITapGestureRecognizer *tapSettingsGesture;
            
            
                        }
            
                        @property (weak, nonatomic) IBOutlet UIImageView *firstImage;
                        @property (weak, nonatomic) IBOutlet UIImageView *lightSettings;
            
                        @property (nonatomic, assign) NSInteger cellIndex;
                        @property (nonatomic, strong) id<CustomTableViewCellDelegate>delegate;
            
                        **CustomTableViewCell.m**
            
                        #import "CustomTableViewCell.h"
            
                        @implementation CustomTableViewCell
            
                        - (void)awakeFromNib {
                            [super awakeFromNib];
                            // Initialization code
            
                            [self addGestures];
                        }
            
                        - (void)addGestures {
                            tapGesture = [[UITapGestureRecognizer alloc] 
                         initWithTarget:self action:@selector(didTapFirstImage:)];
                            tapGesture.numberOfTapsRequired = 1;
                            [_firstImage addGestureRecognizer:tapGesture];
            
                            tapSettingsGesture = [[UITapGestureRecognizer alloc] 
                       initWithTarget:self action:@selector(didTapSettingsImage:)];
                            tapSettingsGesture.numberOfTapsRequired = 1;
                            [_lightSettings 
                        addGestureRecognizer:tapSettingsGesture];
                        }
            
                        - (void)didTapFirstImage:(UITapGestureRecognizer *)gesture 
                       {
                            if (_delegate) {
                                [_delegate didTapFirstImageAtIndex:_cellIndex];
                            }
                        }
            
                        -(void)didTapSettingsImage: (UITapGestureRecognizer 
                        *)gesture {
                            if(_delegate) {
                                [_delegate didTapSettingsAtIndex:_cellIndex];
                            }
                        }
            
            
                     **HomeController.h**
            
                    #import <UIKit/UIKit.h>
                    #import "CustomTableViewCell.h"
            
                    @interface HomeController : CustomNavigationBarViewController 
                    <UITableViewDelegate, UITableViewDataSource, 
                     UIGestureRecognizerDelegate, CustomTableViewCellDelegate>
            
                    @end
            
                    **HomeController.m**
                    ---------------------         
            
                    #import "HomeController.h"
                    #import "CustomTableViewCell.h"
            
            
            
                    @implementation HomeController
            
            
                    -(NSInteger)tableView:(UITableView *)tableView 
                   numberOfRowsInSection:(NSInteger)section {
            
                    return 2 (Number of rows) ;
                     // return number of rows 
                }
            
            
                     -(UITableViewCell *)tableView:(UITableView *)tableView 
                 cellForRowAtIndexPath:(NSIndexPath *)indexPath
                    {
                       CustomTableViewCell *cell = [tableView 
                        dequeueReusableCellWithIdentifier:@"cell" 
                        forIndexPath:indexPath];
            
            
                     cell.delegate = self;
                     cell.cellIndex = indexPath.row;
                     cell.firstImage.userInteractionEnabled = YES;
                     cell.lightSettings.userInteractionEnabled = YES;
            
                     return cell;
                     }
            
            
            
            
                    -(void)didTapFirstImageAtIndex:(NSInteger)index
                    {
                        NSLog(@"Index %ld ", (long)index);
                        //Do whatever you want here
                    }
            
                    -(void)didTapSettingsAtIndex:(NSInteger)index
                    {
                        NSLog(@"Settings index %ld", (long)index);
                        // Do whatever you want here with image interaction
                    }
            
            
            
                    @end
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-04-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-09-08
              • 2023-03-10
              相关资源
              最近更新 更多