选择时图像消失的自定义UITableViewCell

时间:2013-02-21 02:48:46

标签: ios objective-c xcode uitableview

我创建了一个自定义的tableviewcell类,并且单元格加载完美,但是,当我点击它们时,它们会消失并在我点击不同的单元格时返回。谁能帮我理解为什么?提前谢谢!

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell
UIView *backgroundView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
    UIView *visibleBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29, 0, backgroundView.bounds.size.width -58, backgroundView.bounds.size.height)];
    [visibleBackgroundView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"tableViewCell.png"]]];
    [backgroundView addSubview:visibleBackgroundView];
    self.backgroundView = backgroundView;



}
return self;
}

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

// Configure the view for the selected state
if (selected) {
    UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
    UIView *visibleSelectedBackground = [[UIView alloc] initWithFrame:CGRectMake(29, 0, backgroundView.bounds.size.width -58, backgroundView.bounds.size.height)];
    [visibleSelectedBackground setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"selectedTableViewCell@2x.png"]]];
    [selectedBackgroundView addSubview:visibleSelectedBackground];
    self.selectedBackgroundView = selectedBackgroundView;
}
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
    [self setHighlighted:NO];
}
}

1 个答案:

答案 0 :(得分:0)

我能够使用以下解决方案修复它

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell{
    UIView *backgroundView;
    UIView *visibleBackgroundView;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
    visibleBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29, 0, backgroundView.bounds.size.width -58, backgroundView.bounds.size.height)];
    [visibleBackgroundView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"tableViewCell.png"]]];
    [backgroundView addSubview:visibleBackgroundView];
    self.backgroundView = backgroundView;
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];


    }
    return self;
}

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

// Configure the view for the selected state
if (selected) {

}
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
    [visibleBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"selectedTableViewCell.png"]]];
} else{
     [visibleBackgroundView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"tableViewCell.png"]]];
}
}