首页 技术 正文
技术 2022年11月14日
0 收藏 940 点赞 3,326 浏览 3859 个字

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

试着按照指示改了一下,发现还是没能解决问题,最后发现是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:

<img src="https://www.shuzhiduo.com/A/x9J2mYWgz6/

       

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860