简体   繁体   English

围绕UITableView的一部分阴影

[英]Shadow around a section of UITableView

I have a UITableView with 4 sections. 我有一个UITableView有4个部分。 Now I want to add a shadow effect to a particular section of table but not the full table. 现在我想为表的特定部分添加阴影效果,但不是全表。 How can I achieve this task? 我怎样才能完成这项任务?

You will have to alter the sections header and footer as well as all cells of the section. 您将不得不更改部分页眉和页脚以及该部分的所有单元格。

Use tableView:viewForFooterInSection: , tableView:viewForHeaderInSection: and tableView:cellForRowAtIndexPath: for that. 使用tableView:viewForFooterInSection: , tableView:viewForHeaderInSection:tableView:cellForRowAtIndexPath: for tableView:viewForHeaderInSection: Just add a UIView with rgba 0/0/0/0.2 or similar to each view you want to be dark. 只需添加一个带有rgba 0/0/0 / 0.2的UIView或类似于你想要变暗的每个视图。

You can add image with shadow as background of UITableViewCell. 您可以添加带阴影的图像作为UITableViewCell的背景。 Shadow should be drawn in the image. 阴影应该在图像中绘制。 It is very simple and your application will run faster. 它非常简单,您的应用程序运行速度更快。

You can specify the shadow for the the section you want. 您可以为所需的部分指定阴影。 Here is a sample. 这是一个例子。

First of all we are making some space available for your header 首先,我们为您的标题提供了一些空间

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == mysection)
    {
        // Returns the height you want for the header section. I am giving 20
        return 20; 
    }
}

Then the decoration of the header 然后是标题的装饰

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == mysection)
    {
        UIView *shadowView  =  [[[UIView alloc] initWithFrame: CGRectMake(0,0,320,20)] autorelease];
        shadowView.backgroundColor = [UIColor whiteColor];

        // Doing the Decoration Part
        shadowView.layer.shadowColor = [[UIColor blackColor] CGColor];
        shadowView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
        shadowView.layer.shadowRadius = 3.0f;
        shadowView.layer.shadowOpacity = 1.0f;

        return shadowView;
    }
    return nil;
}

Complete it on your side. 完成它在你身边。 This one is a basic outline. 这是一个基本的大纲。 Happy Coding :) 快乐编码:)

Try this 试试这个

yourView.layer.shadowColor = [[UIColor blackColor] CGColor];
yourView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
yourView.layer.shadowRadius = 3.0f;
yourView.layer.shadowOpacity = 1.0f; 

You'll need to replace "yourView" with anything else 您需要将“yourView”替换为其他任何内容

don't forget also you'll need import QuartzCore/CALayer.h 别忘了你还需要导入QuartzCore / CALayer.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM