当前位置: 代码迷 >> Iphone >> 在自定义TableViewCell类里头添加按钮事件触发不了的一些实践
  详细解决方案

在自定义TableViewCell类里头添加按钮事件触发不了的一些实践

热度:77   发布时间:2016-04-25 05:31:40.0
在自定义TableViewCell类里面添加按钮事件触发不了的一些实践

我的自定义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: