8

我有UITableView4 个部分。现在我想为表格的特定部分而不是整个表格添加阴影效果。我怎样才能完成这项任务?

4

4 回答 4

0

您将不得不更改部分的页眉和页脚以及该部分的所有单元格。

使用tableView:viewForFooterInSection:,tableView:viewForHeaderInSection:tableView:cellForRowAtIndexPath:为此。只需添加一个带有 rgba 0/0/0/0.2 或类似于您想要变暗的每个视图的 UIView。

于 2012-11-15T10:30:10.857 回答
0

您可以添加带有阴影的图像作为 UITableViewCell 的背景。应该在图像中绘制阴影。它非常简单,您的应用程序将运行得更快。

于 2012-11-15T10:34:53.487 回答
-1

您可以指定所需部分的阴影。这是一个示例。

首先,我们为您的标题腾出一些空间

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

然后是表头的装饰

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

在你身边完成它。这是一个基本的大纲。快乐编码:)

于 2012-11-15T10:45:53.713 回答
-3

试试这个全表阴影

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

试试这个部分阴影

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if (cell.responds(to: #selector(getter: UIView.tintColor))){
        if tableView == self.tableView {
            let cornerRadius: CGFloat = 12.0
            cell.backgroundColor = .clear
            let layer: CAShapeLayer = CAShapeLayer()
            let path: CGMutablePath = CGMutablePath()
            let bounds: CGRect = cell.bounds
            bounds.insetBy(dx: 25.0, dy: 0.0)
            var addLine: Bool = false

            if indexPath.row == 0 && indexPath.row == ( tableView.numberOfRows(inSection: indexPath.section) - 1) {
                path.addRoundedRect(in: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)

            } else if indexPath.row == 0 {
                path.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
                path.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY), tangent2End: CGPoint(x: bounds.midX, y: bounds.minY), radius: cornerRadius)
                path.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
                path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))

            } else if indexPath.row == (tableView.numberOfRows(inSection: indexPath.section) - 1) {
                path.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
                path.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY), radius: cornerRadius)
                path.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
                path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))

            } else {
                path.addRect(bounds)
                addLine = true
            }

            layer.path = path
            layer.fillColor = UIColor.white.withAlphaComponent(0.8).cgColor

            if addLine {
                let lineLayer: CALayer = CALayer()
                let lineHeight: CGFloat = 1.0 / UIScreen.main.scale
                lineLayer.frame = CGRect(x: bounds.minX + 10.0, y: bounds.size.height - lineHeight, width: bounds.size.width, height: lineHeight)
                lineLayer.backgroundColor = tableView.separatorColor?.cgColor
                layer.addSublayer(lineLayer)
            }

            let testView: UIView = UIView(frame: bounds)
            testView.layer.insertSublayer(layer, at: 0)
            testView.backgroundColor = .clear
            cell.backgroundView = testView
        }
    }
}
于 2012-11-15T10:28:47.833 回答