0

我创建了一个自定义 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];
}
}
4

1 回答 1

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"]]];
}
}
于 2013-02-23T03:34:54.383 回答