Aldee
Aldee

Reputation: 4528

UIView background color does not take effect

Can someone tell me why this is not working right?

I have these lines of code below within my table view cell for tableView:didSelectAtIndexRowPath: method.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableView viewWithTag:199]removeFromSuperview];

    CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size;

    UIView *subSelectionView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, (int)cellSize.width - 20, 100)];

    [subSelectionView setBackgroundColor:[UIColor  blueColor]];

    subSelectionView.layer.borderColor = [UIColor grayColor].CGColor;
    subSelectionView.layer.borderWidth = 1;

    subSelectionView.tag = 199;    

    [[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView];
}

Notice the code:

[subSelectionView setBackgroundColor:[UIColor  blueColor]];

Obviously, I want to change the color of the subview that I added to the UITableViewCell but why is it not working ?

Upvotes: 1

Views: 497

Answers (2)

Ilanchezhian
Ilanchezhian

Reputation: 17478

Add the following line to the start of the method

[tableView deselectRowAtIndexPath:indexPath animated:NO];

So ur method would be

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    [[tableView viewWithTag:199]removeFromSuperview];
    ....
}

Upvotes: 1

Piyush Kashyap
Piyush Kashyap

Reputation: 1965

jus reload the table after adding the subview on the table

[[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView];

[tableview reloadData];

Upvotes: 2

Related Questions