2013-05-01 42 views
2

我有一个自定义的UITableViewCell(从XIB实例化)与图像作为背景。现在,我希望在选择单元格时将另一个图像作为背景(类似于标准单元格的蓝色闪烁)。我已经尝试使用或didSelectRowAtIndexPath中的setSelectedBackgroundView进行设置,但突出显示的背景不会显示出来。我究竟做错了什么?自定义UITableViewCell与图像突出显示

回答

3

在您的自定义Cell.xib

enter image description here

接下来让这个

enter image description here

终于做到这一点

enter code here

+2

THX与图像解释,但你错过了没有设置selectionStyle无法比拟的。 – DanielR 2013-05-01 07:21:18

+0

是啊,对不起..忘记.....(+ 1)为您的问题和评论.... – 2013-05-01 07:26:06

1

您可以使用为CustomCell创建的XIB来设置选定的背景视图。您只需要将UIImageView设置为XIBimageview必须与CustomCell的高度相同)。现在将名为“selectedBackgroundView”的插座连接到CustomCellUIImageView作为选定的背景视图。还要检查的selectionStyle是否不是无。

2

你试过像下面一个

调用下面的方法从TableView中的DataSource方法(cellForAtIndexPath)做同样的工作

- (void)setCellBGColorNormalAndSelectedForTableViewCell:(UITableViewCell*)cell cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    UIImageView *normalCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 

[normalCellBG setImage:[UIImage imageNamed:@"box_nonselected.png"]];//Set Image for Normal 
[normalCellBG setBackgroundColor:[UIColor clearColor]]; 
[cell setBackgroundView:normalCellBG]; 


UIImageView *selecetedCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 
[selecetedCellBG setImage:[UIImage imageNamed:@"box_selected.png"]];//Set Name for selected Cell 
[selecetedCellBG setBackgroundColor:[UIColor clearColor]]; 
[cell setSelectedBackgroundView:selecetedCellBG ]; 

} 
+0

老兄,我有一个相同的问题。你的回答很好,但是如果我们在'cellForAtIndexPath'中写入并且图像被覆盖,这个方法会被调用很多次。如何只打一次电话? – Krunal 2014-01-06 12:01:42

相关问题