bruno bruno - 1 year ago 74
iOS Question

View in UiTableViewCell turns transparent at selection

I have a custom

with a Red
which turns transparent when i select the

I donĀ“t want that to happen. Is there a way to keep the color of the
at selection?

Only configuration regarding that view.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *kCellIdentifier = @"Cell Identifier";
DetailsCell *cell = (DetailsCell *) [tableView dequeueReusableCellWithIdentifier: kCellIdentifier];

if (cell == nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DetailsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.myView.layer.cornerRadius = 5;
cell.myView.layer.masksToBounds = YES;
return cell;

Answer Source

Add tag to your view.

yourview.tag = 3;

And than in your tableView's didSelectRowAtIndexPath method add this code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    //get the cell which is selected
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    //set tempView color of selected cell bcoz when cell selected all view color is gone
    UIView *tempView=[selectedCell viewWithTag:3];
//set your color whatever you want
    tempView.backgroundColor = [UIColor colorWithRed:(112/255.0) green:(136/255.0) blue:(213/255.0) alpha:1];


You can also do this using selecting your cell selection color to NONE. Put this code in cellForRowAtIndexPath

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download