2010-02-15 48 views
1

Iam创建自定义TableView单元格,其中包含一些文本和背景图像。当我选择细胞然后图像应chnage但这不是hapening,我有使用下面的代码,所以有人帮助我?UITableView自定义单元格选择问题

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 

[super setSelected:selected animated:animated]; 

if (selected) 
    [self.imgSelected setImage:[UIImage imageNamed:@"listing-gradient-hover.png"]];//Image that i want on Selection 
else 
    [self.imgSelected setImage:[UIImage imageNamed:@"listing-gradient.png"]]; 
//Normal image at background everytime table loads. 

    // Configure the view for the selected state 

} 

这不适合我。

回答

4

尝试以下方法:

初始化一个UIImageView为背景视图和UIImageView是选择的背景图时设置YPU定制表格视图细胞:

self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"listing-gradient-hover.png"]]; 
self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"listing-gradient.png"]]; 

UITableViewCell增加的值仅当选中单元格时,selectedBackgroundView属性才作为子视图。它将所选背景视图作为子视图直接添加到背景视图(backgroundView)之上(如果它不是零),或者在所有其他视图之后。调用setSelected:animated:会导致选定的背景视图使用alpha淡入淡出和淡出。您不需要重写此方法仅用于在选择后切换背景图像。

+0

可能要autorelease那些。 mem泄漏! – ninjaneer 2011-09-04 02:59:05