简体   繁体   中英

UITableViewCell Set Selected Image

trying to figure out how to set the selected image for for a tableViewCell .

The old way of writing this was cell.selectedImage but that has been deprecated since 3.0.

I've tried numerous things, but can't get it too work.

Thanks! Josh

According to the Deprecated UITableViewCell Methods doc, you should use the imageView's highlightedImage property as a replacement for selectedImage. For example:

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

You can set selected backgroundView like below....

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

That didn't worked because you might set the normal state image like this

cell.imageView.image = ///

Try it - It worked for me perfectly!!

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

Try this ...

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

Hope it will solve your problem !

Use this one!:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM