dandan
dandan

Reputation: 509

UITableViewCell with semi transparent selected background

I need to hide the normal (unselected - cell.backgroundView) image of a cell when the cell is selected and show it when it is not selected.

The way the tableview works is that the normal view (cell.backgroundView) is always there and when the cell is selected it animates the selected image (cell.selectedBackgroundView) into view and places on top of the normal view.

The problem is when the selected cell is semitransparent and the normal cell is always visible underneath it. I created, in 2 views for my (custom) UITableViewCell which I load in my view controller:

-(void)tableView:(UITableView *)tableView 
 willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"XYCell"]];
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"XYCellSelected"]];
}

I cleared colors from in the required places but I cannot get it to work as I want. Since my selected images (cell.selectedBackgroundView ) is semitransparent, the cell.backgroundView can still be seen underneath it. How can I make it go away?

Upvotes: 0

Views: 1424

Answers (2)

STAR_ZERO
STAR_ZERO

Reputation: 1460

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.highlightIndexPath = indexPath;    // for iOS6
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundView.hidden = YES;
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row == NSNotFound) {
        // for iOS6
        cell = [tableView cellForRowAtIndexPath:self.highlightIndexPath];
    }
    cell.backgroundView.hidden = NO;
}

Upvotes: 1

Daniel Parlapan
Daniel Parlapan

Reputation: 46

In general, if you want a custom cell you should implement your own uitableviewcell.

In your case take a look at

- (void)setSelected:(BOOL)selected animated:(BOOL)animated;

Code example to help you :

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
   [super setSelected:selected animated:animated];
   //your own backgroundview when selected
   self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectedBck.png"]];
   if (selected){
        // edit the cell's view when it's selected
        self.backgroundView = nil;
    } 
    else {
       // edit the cell's view when it isn't selected
    }
}

Upvotes: 1

Related Questions