UITableViewCell上控件的点击事件

zzmei 2017-07-08 12:52:08
UIImageView *imageView;

UIView *view;
[view addSubview:imageView];

[UITableviewCell.contentView addSubview:view];

在列表单元格上添加这样一个图片视图,怎样获取到这个图片视图(imageView)的点击事件?
...全文
689 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_28472349 2017-07-25
  • 打赏
  • 举报
回复
重写tableviewcell @interface TableViewCell : UITableViewCell @property (nonatomic, strong) UIImageView *imgView; @property (nonatomic, copy) void (^Block)(BOOL isSelected); - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetHeight(self.contentView.bounds), CGRectGetHeight(self.contentView.bounds))]; self.imgView.backgroundColor = [UIColor redColor]; self.imgView.userInteractionEnabled = YES; [self.contentView addSubview:self.imgView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; [self.imgView addGestureRecognizer:tap]; } return self; } - (void)tap:(UITapGestureRecognizer *)tap { if (self.Block) { self.Block(YES); } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"]; cell.Block = ^(BOOL isSelected) { NSLog(@"%ld", indexPath.row); }; return cell; }
Quinn士魁 2017-07-14
  • 打赏
  • 举报
回复
试试这个 let position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableview) if let indexPath = self.tableview.indexPathForRowAtPoint(position) { self.tableview.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) }
zzmei 2017-07-11
  • 打赏
  • 举报
回复
有人遇到过这样的问题吗?
zzmei 2017-07-10
  • 打赏
  • 举报
回复
引用 4 楼 tubo_true 的回复:
解决方案,加手势 和 frame ,autolayout 无关
按照上面的代码要怎么加?我上面的代码已经加了监听了,但还是没效果。
tubo_true 2017-07-10
  • 打赏
  • 举报
回复
解决方案,加手势 和 frame ,autolayout 无关
zzmei 2017-07-10
  • 打赏
  • 举报
回复
顶一下 顶一下
zzmei 2017-07-08
  • 打赏
  • 举报
回复
我试了一下,用Frame的方式创建视图就可以监听到;但换成Autolayout方式就不行,代码如下: ClickTv.m
#import "ClickTv.h"

@implementation ClickTv

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 66.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *IDENTIFIER = @"CLICK_TV_CELL";
    
    ClickTvCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
    if(nil == cell)
        cell = [[[ClickTvCell alloc] init] autorelease];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame style:UITableViewStylePlain];
    if(self)
    {
        self.delegate = self;
        self.dataSource = self;
    }
    
    return self;
}

@end
ClickTvCell.m

#import "ClickTvCell.h"

@implementation ClickTvCell

- (void)uiInitialize
{
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    
    self.view = [[ClickTvCellContentView alloc] init];
    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.contentView addSubview:self.view];
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f]];
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0f constant:0.0f]];
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0f constant:200.0f]];
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0f constant:44.0f]];
}

- (id)init
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CLICK_TV_CELL"];
    if(self)
    {
        [self uiInitialize];
    }
    
    return self;
}

@end
ClickTvCellContentView

#import "ClickTvCellContentView.h"

@implementation ClickTvCellContentView

- (void)ivClick:(id)sender
{
    //此处监听不到
    NSLog(@"========");
}

- (void)uiInitialize
{
    self.iv = [[UIImageView alloc] init];
    self.iv.translatesAutoresizingMaskIntoConstraints = NO;
    self.iv.userInteractionEnabled = YES;
    self.iv.image = [UIImage imageNamed:@"icon_my_score_normal"];
    [self addSubview:self.iv];
    
    UITapGestureRecognizer *gestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ivClick:)] autorelease];
    [self.iv addGestureRecognizer:gestureRecognizer];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iv attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0f constant:0.0f]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iv attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iv attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:0.2f constant:0.0f]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iv attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f]];
}

- (id)init
{
    self = [super init];
    if(self)
    {
        [self uiInitialize];
    }
    
    return self;
}

@end
不担心 2017-07-08
  • 打赏
  • 举报
回复
可以在view或者imageView上添加一个tap手势,如果是添加在imageView上,你要设置imageView的userInteractionEnabled为true UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)]; [self.ImageView addGestureRecognizer:tap]; -(void)onTap:(id)sender { do something.... }

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧