DanielR
DanielR

Reputation: 711

custom UITableViewCell with image for highlighting

I have a custom UITableViewCell (instantiated from XIB) with an image as background. Now I want another image to be the background when the cell is selected (similar to the blue flashing for standard cells). I already tried setting it using setSelectedBackgroundView in (void)setSelected:(BOOL)selected animated:(BOOL)animated or even in didSelectRowAtIndexPath but the background for highlighting wont show up. What am I doing wrong?

Upvotes: 2

Views: 1334

Answers (3)

Mohammad Kamar Shad
Mohammad Kamar Shad

Reputation: 6067

have you tried like below one

Call below Method From the TableView DataSource Method (cellForAtIndexPath) For Doing the same Task

 - (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 ];

 }

Upvotes: 2

Manu
Manu

Reputation: 4750

In your Custom Cell.xib

enter image description here

Next make this

enter image description here

And Finally do this

enter code here

Upvotes: 3

Gaurav Rastogi
Gaurav Rastogi

Reputation: 2145

You can set selected background view using the XIB you created for CustomCell . You just need to take UIImageView on to the that XIB (imageview must have same height as that of CustomCell) . Now connect the outlet naming "selectedBackgroundView" for that CustomCell to the UIImageView you have taken as a selected background view . Also check whether the selectionStyle for that CustomCell is not none .

Upvotes: 1

Related Questions