Pregunta

I am trying to create inner shadow for UITableViewCell at right side alone. This is how I do,

if (![cell viewWithTag:100]) {
UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH, 0, 10, height)];
shadowView.layer.shadowColor = [UIColor darkGrayColor].CGColor;        
shadowView.layer.shadowRadius = 5.0;
shadowView.layer.shadowOffset = CGSizeMake(-2, 0);
shadowView.layer.shadowOpacity = 0.8;
shadowView.backgroundColor = [UIColor darkGrayColor];
shadowView.tag = 100;

shadowView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

[cell addSubview:self.shadowView];
}

But here my problem is each time when I scroll the shadow is getting darker. Also goes out of cell bounds and spoils the cell design. I suspect its getting added repeatedly. Can anyone help me to solve this issue ? I am not interested to use image for shadow. So any other solution than using image will be appreciated. Thanks in advance.

¿Fue útil?

Solución 2

I found the problem. I should have done,

self.contentView.superview.clipsToBounds = YES;
    self.contentView.clipsToBounds = YES;

Now everything works like charm.

Otros consejos

Make sure that you are using dequeueReusableCellWithIdentifier properly. the following code works fine for me

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

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        NSLog(@"new cell");

            UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 10, 44)];
            shadowView.layer.shadowColor = [UIColor darkGrayColor].CGColor;
            shadowView.layer.shadowRadius = 5.0;
            shadowView.layer.shadowOffset = CGSizeMake(-2, 0);
            shadowView.layer.shadowOpacity = 0.8;
            shadowView.backgroundColor = [UIColor darkGrayColor];
            shadowView.tag = 100;
            shadowView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

            [cell addSubview:shadowView];
    }
    else
    {
        NSLog(@"old cell");
    }

    return cell;
}

every time you scroll cell refresh and view got added again you can two thing

one make sure shade is not added and you are not adding it again.

or

Remove all sub views and add them again when you create cells

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top