Fred Collins
Fred Collins

Reputation: 5010

Adding drop shadow to UITableView doesn't work properly

I've created a sample app for adding drop shadow to a UITableView. When you click on the right navigation button a drop shadow is added to the table. The problem is that the shadow is added only to the part of the table visible in the screen. If I try to scroll up (and go off the table) or scroll down and see others cell the shadow is not visible. How can I set a shadow for the entire height of the table? If is possible I would have the shadow also if I scroll up and go off the table (for the bounce effect). I attach either two screenshot and the code.

In the first screenshot I scroll down to see others cell, in the second one I scroll up for triggering the default UITableView bounce effect.

enter image description hereenter image description here

Here's the code:

- (void)printIt:(id)sender
{
    [self.tableView.layer setShadowColor:[[UIColor orangeColor] CGColor]];
    [self.tableView.layer setShadowOffset:CGSizeMake(0, 0)];
    [self.tableView.layer setShadowRadius:15.0];
    [self.tableView.layer setShadowOpacity:0.8];
    [self.tableView.layer setMasksToBounds:NO];
    self.tableView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.tableView.layer.bounds].CGPath;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *testButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(printIt:)];
    self.navigationItem.rightBarButtonItem = testButton;
    [testButton release];
}

#pragma mark UITableView methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 105;
}

Upvotes: 0

Views: 2973

Answers (1)

rob mayoff
rob mayoff

Reputation: 385590

The easiest way to handle this is to embed the table view inside a UIView, and set the shadow on the UIView.

Upvotes: 5

Related Questions