向UITableView添加阴影无法正常工作 [英] Adding drop shadow to UITableView doesn't work properly

查看:216
本文介绍了向UITableView添加阴影无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个示例应用程序,用于向UITableView添加阴影.当您单击右侧导航按钮时,会将一个阴影添加到表格中.问题在于阴影仅添加到屏幕上可见的表格部分.如果我尝试向上滚动(然后离开桌子)或向下滚动并看到其他单元格,则看不到阴影.如何为桌子的整个高度设置阴影?如果可能的话,如果我向上滚动并离开桌子(反弹效果),我也会有阴影.我附上了两个屏幕截图和代码.

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.

在第一个屏幕截图中,我向下滚动以查看其他单元格,在第二个屏幕截图中,我向上滚动以触发默认的UITableView反弹效果.

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.

代码如下:

- (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;
}

推荐答案

最简单的方法是将表格视图嵌入UIView内,并在UIView上设置阴影.

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

这篇关于向UITableView添加阴影无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆