Myaaoonn
Myaaoonn

Reputation: 997

Image set on UItableviewCell

I have three images: Normal, highlighted, and selected. How do I set it in a Tableview custom cell?

I've tried this:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if (selected) {
        self.highlighted = NO;
        self.imgBackgroundCell.image = [UIImage imageNamed:[MTUtility setImageWithName:@"listselected"]];
    }

} 
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animate {
    if (highlighted) {
        self.imgBackgroundCell.image = [UIImage imageNamed:[MTUtility setImageWithName:@"listpressed"]];
    }
}

But highlighted remains as it is many times.

enter image description here

Upvotes: 0

Views: 67

Answers (2)

Calin Chitu
Calin Chitu

Reputation: 1099

Try updating your code:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if (selected) {
        self.highlighted = NO;
        self.imgBackgroundCell.image = [UIImage imageNamed:[MTUtility setImageWithName:@"listselected"]];
    }
    else{
       ...unselect code
    }

} 
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animate {
    if (highlighted) {
        self.imgBackgroundCell.image = [UIImage imageNamed:[MTUtility setImageWithName:@"listpressed"]];
    }
    else{
      ...unhighlight
    }
}

Upvotes: 1

z22
z22

Reputation: 10083

If you want the selected image on didselectrowatindexpath, then you can set UITableviewCell's property setSelectedBackgroundView in cellForRowAtIndexPath method as follows:

    - (UITableViewCell*)tableView:(UITableView*)tableView 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 ];

}

Let me know if this is what you need to do.

Upvotes: 0

Related Questions