检测在UITableViewCell内点击UIImageView

时间:2013-11-04 05:11:52

标签: ios objective-c uitableview uiimageview uigesturerecognizer

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

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

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

  3. 这是我到目前为止所尝试的内容。

    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
    }
    

    点击时不调用此方法。我已添加到我的头文件中。

    提前感谢您的帮助。

6 个答案:

答案 0 :(得分:14)

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

因此,代码中的某个位置会生成数组

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

然后,当您设置tableview单元格时,将标记设置为行#

//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];    
}

答案 1 :(得分:3)

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

然后在操作选择器中,执行以下步骤以了解已点击的视图:

-(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
}

答案 2 :(得分:0)

手势识别器只会将一个参数传递给动作选择器:本身。所以你需要单独传递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 .
}

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

答案 3 :(得分:0)

您可以使用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'

答案 4 :(得分:0)

使用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,则可能需要缩短代码。

答案 5 :(得分:0)

在这里,我们有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