10

我有一个自定义的复杂 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
}

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

提前感谢您的帮助。

4

6 回答 6

14

也许不是理想的解决方案,但为每个 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];    
}
于 2013-11-04T06:17:46.760 回答
4

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

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

-(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
}
于 2016-02-10T18:50:41.687 回答
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 .
}

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

于 2013-11-04T07:35:34.423 回答
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'
于 2016-12-27T15:12:39.267 回答
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.

于 2017-10-17T09:46:28.740 回答
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
于 2018-02-01T11:16:48.833 回答