Search code examples
objective-cipadios7uitableviewcalayer

UITableViewCell selectedBackground shadow not displayed on device


Image showing shadow for selected cell on Simulator but not on Device

This one has me thrown and I'm wondering if anyone can help. I am trying to display a shadow on my selected UITableViewCell and it works fine on the Simulator but not on my iPad 3rd Gen running iOS 7.0.4

I have a subclassed UITableViewCell which is alloc/initting a subclassed UIView as its selectedBackground. All works fine other than it not displaying on the Device.

In my subclassed UITableViewCell...

- (void)awakeFromNib
{
    self.selectedBackgroundView = [[OTHD_CellSelectedView alloc] initWithFrame:self.bounds];

}

And in my OTHD_CellSelectedView...

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createLayer];
    }
    return self;
}

- (void) createLayer
{
    CALayer *the_layer = [CALayer layer];
    [self.layer insertSublayer:the_layer atIndex:0];

    the_layer.frame = self.bounds;
    the_layer.backgroundColor = [[UIColor whiteColor] CGColor];
    the_layer.shadowColor = [UIColor blackColor].CGColor;
    the_layer.shadowOffset = CGSizeMake(0, 0);
    the_layer.shadowRadius = 10.0;
    the_layer.shadowOpacity = 0.7;
}

Solution

  • I think you should bringToFront the cell in Table I have tried this and it is working for me... I hope this will help you:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 10;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
        if (!cell)
        {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"Cell"];
        }
    
        if (indexPath.row == selected)
        {
            cell.layer.backgroundColor = [[UIColor whiteColor] CGColor];
            cell.layer.shadowColor = [UIColor blackColor].CGColor;
            cell.layer.shadowOffset = CGSizeMake(0, 0);
            cell.layer.shadowRadius = 10.0;
            cell.layer.shadowOpacity = 0.7;
    
            [cell.superview bringSubviewToFront:cell];
        }
        else
        {
            cell.layer.shadowOpacity = 0;
        }
    
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        selected = indexPath.row;
        [tableView reloadData];
    
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:(UITableViewRowAnimationNone)];
    }