2011-02-14 51 views
16

试图找出如何设置为tableViewCell选定的图像。UITableViewCell设置选定图像

写这个的旧方法是cell.selectedImage但自3.0以来已弃用。

我已经尝试了很多事情,但无法得到它的工作。

谢谢! 乔希

回答

9

可以设置选择backgroundView像下面....

 UIImageView *selBGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"urimage.png"]]; 
       cell.selectedBackgroundView = selBGView; 
+0

已经尝试过,一个... 这是结果 - http://cl.ly/2p1X0v3V1d2E133P3q0c – 2011-02-14 04:43:01

2

尝试......

UIImage *bgImage = [UIImage imageNamed:@"icon_call.png"]; 
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgImage]; 

    [bgImageView setFrame:CGRectMake(280, 2, 30, 38)]; 

//Finally give this imageView to the cell 

    [cell.contentView addSubview:bgImageView]; 

希望这将解决您的问题!

+0

真棒!多谢,伙计。 CGRect coords有点不合适,但我无法修复。感谢您的帮助:) – 2011-02-14 04:47:26

+0

@Joshua Russell欢迎您! – Sudhanshu 2011-02-14 05:04:58

4

这并没有奏效,因为你可能设定的正常状态图像这样

cell.imageView.image = /// 

试试吧 - 它为我工作完美!

UIImageView *selBGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"urimage_selected.png"]]; 
cell.selectedBackgroundView = selBGView; 
[selBGView release]; 

UIImageView *BGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"urimage.png"]]; 
cell.backgroundView = BGView; 
[BGView release]; 
1

使用这一个!:

selectionBackground = [UIImage imageNamed:@"background.png"]; 
cell.selectedBackgroundView =[[UIImageView alloc] init]; 
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; 
24

按照Deprecated UITableViewCell Methods文档,你应该使用的ImageView highlightedImage财产作为selectedImage的替代品。例如:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 
cell.imageView.image = [UIImage imageNamed:@"deselected_image.png"]; 
cell.imageView.highlightedImage = [UIImage imageNamed:@"selected_image.png"]; 
相关问题