我的自定义cell上面有5个控件,分别是一个背景的UIImageView,一个专辑的UIImageView(上面加了一个播放的button),一个专辑名字的UIImageView(上面加了显示标题的UILabel)。当我给button绑定点击事件的时候,发现点击的方法触发不了,百度了一下,网上都说是系统的问题,说是iOS7之后cell 的View变了,

试着按照指示改了一下,发现还是没能解决问题,最后发现是UIImageView的用户交互的问题,因为UIImageView的用户交互是默认关闭的,加在它上面的控件自然也响应不了事件。只要把UIImageView的userInteractionEnabled改为YES就能解决问题了。

附录:

自定义的UITableViewCell.h

#import <UIKit/UIKit.h>

@protocol PlayButtonDelegate <NSObject>

-(void)playButtonAction:(UIButton*)sender;

@end

@class Album;
@interface FMTableViewCell : UITableViewCell
@property(strong,nonatomic)UIImageView* typeImage;
@property(strong,nonatomic)UIImageView* typeName;
@property(strong,nonatomic)UIImageView* backImage;
@property(strong,nonatomic)UIButton* playButton;

@property(strong,nonatomic)UILabel* nameLabel;
@property(strong,nonatomic)Album* album;
@property(nonatomic)id<PlayButtonDelegate>delegate;

@end

自定义的UITableViewCell.m

#import "FMTableViewCell.h"  
#import "UIImageView+WebCache.h" // SDWebImage类库
#import "Album.h"   //  model模型
@implementation FMTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.typeImage  = [[UIImageView alloc] init];
        self.typeImage.backgroundColor = [UIColor yellowColor];
        self.typeName = [[UIImageView alloc] init];
        self.typeName.backgroundColor = [UIColor greenColor];
        self.nameLabel = [[UILabel alloc] init];
//        self.nameLabel.backgroundColor = [UIColor orangeColor];
        self.backImage = [[UIImageView alloc] init];
        self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
       
        [self.contentView addSubview:self.backImage];
        
        [self.backImage addSubview:self.typeName];
        [self.backImage addSubview:self.typeImage];
        [self.typeName addSubview:self.nameLabel];
        
//        UIImageView的用户交互要打开,不然添加在它上面的控件不能响应事件,不过cell的点击方法可以实现
        self.backImage.userInteractionEnabled = YES;
        self.typeImage.userInteractionEnabled = YES;
        
        [self.typeImage addSubview:self.playButton];
        [self.playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchDown];
 
    }
    return self;
}

-(void)playButtonAction:(UIButton*)sender
{
    NSLog(@"我的自定义按钮呢");
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.backImage.frame = self.contentView.frame;
    self.backImage.backgroundColor = [UIColor orangeColor];
    self.backImage.image = [UIImage imageNamed:@"22.png"];

CGFloat width = self.backImage.frame.size.width;
    CGFloat height = self.backImage.frame.size.height;
    self.typeImage.frame = CGRectMake(height/12, height/5, 3*height/5, 3*height/5);
    
    self.typeName.frame = CGRectMake(CGRectGetMaxX(self.typeImage.frame)+10, height/6, width-CGRectGetMaxX(self.typeImage.frame)-25, 4*height/6);
    self.typeName.image = [UIImage imageNamed:@"ming"];
    
    self.typeImage.layer.cornerRadius = self.typeImage.frame.size.width/2;
    self.typeImage.clipsToBounds = YES;
    self.typeImage.layer.borderWidth = 1;
   
    self.typeName.layer.cornerRadius = 9;
    self.typeName.clipsToBounds = YES;
    self.typeName.layer.borderWidth = 1;
    
    self.nameLabel.frame = self.typeName.bounds;

self.playButton.frame = CGRectMake(3*self.typeImage.bounds.size.height/8, 3*self.typeImage.bounds.size.height/8, self.typeImage.bounds.size.height/4, self.typeImage.bounds.size.height/4);
    [self.playButton setImage:[[UIImage imageNamed:@"playButton2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
}

-(void)setAlbum:(Album *)album
{
    _album = album;
    
    self.nameLabel.text = self.album.title;
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    self.nameLabel.numberOfLines = 0;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end

cell的UI:

 
 
 
 
 
 
 

在自定义TableViewCell类里面添加按钮事件触发不了的一些实践的更多相关文章

  1. Android中Touch事件分析--解决HorizontalScrollView滑动和按钮事件触发问题

    之前写过关于HorizontalScrollView滑动和按钮事件触发问题,但是不能所有的情况,最近几天一直在想这个问题,今天有一个比较好的解决思路,最终应用在项目里面效果也很好,首先说明一下功能: ...

  2. java swing 按钮事件触发两次或者多次

    按钮事件触发多次? 如果是JButton,八成是由于粗心,多次添加了监听事件 保持只添加一个监听事件就解决了~

  3. android安卓开发基础小笔记,添加按钮事件,打开新窗体,窗体传值,回传

    给一个按钮添加onclick事件 //获取按钮对象 Button Aiyo = (Button)findViewById(R.id.button1); Aiyo.setOnClickListener( ...

  4. Java Hello World例子和添加按钮事件与功能

    新建android工程,然后默认“下一步”即可完成创建: 2.添加Button 3.在src的MainActivity.java添加以下红色代码 import android.support.v7.a ...

  5. 【DevExpress】GridControl添加按钮列并添加按钮事件

    在GridControl中添加按钮列的步骤如下: 1. 把列的ColumnEdit属性设置为RepositoryItemButtonEdit 2. 把TextEditStyle属性设置为HideTex ...

  6. AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮

    AJ分享,必须精品 一:效果图 自定义tabBar实现最下面中间的添加按钮 二:思路 首先在自己的tabBarController中把系统的tabBar设置成自己的tabBar(NYTabBar),这 ...

  7. ExtJs 扩展类CheckColumn的使用(事件触发)

    [javascript] view plain copy print? 使用 Extjs 在进行数据库编程经常会遇到 checkbox 的问题(奇怪网上却没有此类文章不知道其他人是怎么解决的,在此贴上 ...

  8. jquery给img添加按钮事件

    1. img控件加id <a href="#"><img width="20" height="20" id=" ...

  9. 基于VS2013的MFC窗体按钮事件触发案例(亲测可用)

    学过python的小朋友们一定对python freeze命令不陌生,这一命令用于导出python安装模块,用于新电脑可以快速的配置安装所需的模块,以便快速的加入项目. 那么我们大可以用 window ...

随机推荐

  1. [问题记录.VisualStudio]VS2013无法新增和打开项目

    [问题描述] 1) 打开项目失败,报“项目文件只读”或“空引用”错误. 2) 无法新建项目,没有任何可用模板. 3) TFS都正常 [问题产生] 机器环境: 1) 装的Win10双系统,其中一个系统是 ...

  2. package、import和import static

    package 语句: 该语句必须作为源文件的第一条非注释性语句,一个源文件只能指定一个包,即只能包含一条package语句. import 和import static 关键字: 引入import关 ...

  3. C# mvc DropDownList选中状态无效情况分析

    情况: DropDownList控件使用List<SelectListItem>()设置下拉选项和默认值.当控件的Name和后台的ViewBag(或ViewData)的Key重复,会导致选 ...

  4. csrf跨站请求伪造

    如何杜绝跨站请求伪造? 1.要让服务器知道本次请求是不是冒用了用户的身份→ 2.服务器发给用户一个凭证,用户请求时需携带此凭证→ 3.此凭证只能用户看到而且冒用者看不到→ 4.这就用到了浏览器的安全机 ...

  5. Python.with.context-manager

    Context Manager 1. Context Manager简介 "Context managers are a way of allocating and releasing so ...

  6. 「脑洞」图片转HTML(支持动画)

    也许是受到很久以前看到的这玩意儿的原因:The Shapes of CSS 现在开脑洞写了个自动转换,顺便支持了动画……嗯,纯 CSS (:з」∠) 主要步骤就是用 Python 读图片,然后把像素全 ...

  7. 字符串对象-String

    新建字符串对象 ① 直接赋值 ② 构造函数 ③ 转换函数 1    length              字符串对象属性 2    match()  null     跟php中的preg_matc ...

  8. ExtJS4插件EditArea

    EditArea是一个支持语法高亮的文本编辑器,同类软件还有Ace, CodeMirror等.具体功能方面的差异,请访问http://en.wikipedia.org/wiki/Comparison_ ...

  9. MYSQL使用mysqldump导出某个表的部分数据

    命令格式如下: mysqldump -u用户名 -p密码 数据库名 表名 --where="筛选条件" > 导出文件路径 例子: 从meteo数据库的sdata表中导出sen ...

  10. orale 函数大全[转]

    oracle函数大全 http://wenku.baidu.com/link?url=bXaGsnn8iN264GB8ec48IUPg5eRGDKAyAiSw0OBKL1I0mBVG549-2u9HT ...