bruno bruno - 20 days ago 8
iOS Question

View in UiTableViewCell turns transparent at selection

I have a custom

UITableViewCell
with a Red
UIView
which turns transparent when i select the
UITableViewCell
.

I donĀ“t want that to happen. Is there a way to keep the color of the
UIView
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

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

UPDATE

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

cell.selectionStyle = UITableViewCellSelectionStyleNone;