2013-02-21 65 views
0

我創建了一個自定義的tableviewcell類和單元格加載完美,但是,當我點擊它們時,他們消失,當我點擊不同的單元格時回來。任何人都可以幫我理解爲什麼?提前致謝!自定義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:@"[email protected]"]]]; 
    [selectedBackgroundView addSubview:visibleSelectedBackground]; 
    self.selectedBackgroundView = selectedBackgroundView; 
} 
} 

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

解決它,你可以設定亮點是,看看會發生什麼? – 2013-02-21 02:58:12

+0

如果我將突出顯示設置爲「是」,它會在單元格消失之前短時間變爲藍色。 – badger0053 2013-02-21 03:18:07

+1

您是否檢查過[UIImage imageNamed:@「[email protected]」]是否返回任何內容? – rdelmar 2013-02-21 03:50:23

回答

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"]]]; 
} 
}