Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Transparent Accessory View in UITableViewCell? (w/ Screenshot)

I use a custom UITableViewCell from a nib. The accessory view is a Detail Disclosure Indicator . The problem is that the background color of the UITableViewCell behind the accessory view is not getting rendered (see image / source below). Any clues? Also, here are some things that I tried but did NOT work:

Things that DID NOT work:
- Setting the backgroundColor of the accessory view to clearColor
- Setting the contentView.opaque of the cell to FALSE
- Setting the contentView.opaque of the Table View to FALSE
- Setting a non-default accessory view for the cell


alt text http://www.chicknchoke.com/so/IMG_8028.png

    -(void)showTablePrep
    {
        myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, 320, 416) style:UITableViewStylePlain];
        myTableView.dataSource = self;
        myTableView.delegate = self;
        myTableView.delaysContentTouches = FALSE;
        myTableView.opaque = FALSE;
        myTableView.rowHeight = 60;

        [UIView beginAnimations:@"SlideUp" context:nil];
        [UIView setAnimationDuration:0.3f];

        [myTableView setCenter:CGPointMake(myTableView.center.x, myTableView.center.y-436)];
        [self.view addSubview:myTableView];
        [self.view bringSubviewToFront:myTableView];

        [UIView commitAnimations];


    }




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    FriendsCell* cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellID"];

    if (cell == nil){

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FriendsCellView" owner:self options:nil];

        cell = (FriendsCell*)[nib objectAtIndex:0];

        if(indexPath.row % 2 == 0){

        cell.contentView.backgroundColor = [UIColor grayColor];


        }else{

        cell.contentView.backgroundColor = [UIColor lightGrayColor];


        }
        cell.accessoryView.backgroundColor = [UIColor clearColor];
        cell.contentView.opaque = FALSE;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;


    if(f

riendsCounter > indexPath.row){


            cell.titleLabel.text = @"Label";
            cell.descLabel.text = @"Description goes here";

        }else{

            cell.titleLabel.text = @"";
            cell.descLabel.text = @"";
            cell.accessoryType = UITableViewCellAccessoryNone;

        }

        }

        return cell;    
    }
like image 844
RexOnRoids Avatar asked Oct 01 '09 12:10

RexOnRoids


2 Answers

You're drawing the background color for your cell incorrectly. A UITableViewCell will be arranged so that the contentView and accessoryView sit side-by-side. (This is done so that the contentView can clip its content so it doesn't overlap with the accessory view) The problem is not that the accessory view is opaque, it's that the gray background is simply not drawn behind the accessory view.

The correct way of customizing the background drawn behind a UITableViewCell is to customize its backgroundView. I haven't tried this, but since you're only changing the color, you might be able to simply set the backgroundColor color on the backgroundView to your desired color.

like image 71
Alex Avatar answered Oct 31 '22 08:10

Alex


I found the answer by having a look at the subviews of my custom table view cell.

It seems like the accessory view has a button sitting over it. By finding this button in the subviews and changing its color, i was able to update the background color behind the accessory button.

<UIButton: 0x3b4d690; frame = (277 0; 43 75); opaque = NO; layer = <CALayer: 0x3b3e0b0>>

for (UIView *aSubView in self.subviews) {
    if ([aSubView isMemberOfClass:[UIButton class]]) {
        aSubView.backgroundColor = [UIColor greenColor];
    }
}

Unfortunately I was only able to reach this button within the

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

method of my custom table view cell class. I've used this successfully within my app to display a different highlight color when the user selects a cell. This should point you in the right direction.

like image 1
Cliff Viegas Avatar answered Oct 31 '22 07:10

Cliff Viegas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!