2010-06-29 82 views
1

我试图覆盖自定义UITableViewCell子类中的-selectedBackgroundView的访问器。但是,单元格被选中时,单元格将继续使用默认的蓝色选择背景。如何改变?如何更改UITableViewCell的蓝色选择?

我其实很确定这个蓝色的选择是selectedBackgroundView ...还有什么应该对这个具有浅色渐变的蓝色背景负责?但奇怪的是,contentView高于此值,虽然它具有除[UIColor clearColor]之外的backgroundColor,但这个selectedBackgroundView仍然似乎闪耀着光芒。真奇怪。

回答

7

你只是把这个代码放到“的cellForRowAtIndexPath”

对于禁用的细胞中进行选择:(同时单击单元格)

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

为了使细胞:(同时单击单元格)

cell.selectionStyle = UITableViewCellSelectionStyleBlue;(By Default) 

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

如果你想给你的自定义颜色,然后在的cellForRowAtIndexPath”

if (cell==nil) 

{ 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 
UIView *v = [[[UIView alloc] init] autorelease]; 
    v.backgroundColor = [UIColor redColor]; 
    cell.selectedBackgroundView = v; 
} 

一切顺利。

1

听起来像是你只是想

self.selectionStyle = UITableViewCellSelectionStyleNone; 

添加到您的自定义类的初始化。如果你想做任何重型处理,你也可以重写setSelected:animated:方法。

+0

它应该是cell.selectionStyle = UITableViewCellSelectionStyleNone;而不是self.selectionStyle = UITableViewCellSelectionStyleNone; – 2012-05-01 11:42:38