如何在tableview的部分制作边框

时间:2015-11-16 12:41:29

标签: ios objective-c

我必须在ios的tableview部分制作阴影。我试图用这种表格单元格方法来实现这个目的但是它不起作用。

(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Section:%ld  row:%ld", indexPath.section, (long)indexPath.row);

    if ([cell respondsToSelector:@selector(tintColor)])
    {

        CGFloat cornerRadius = 5.f;

        cell.backgroundColor = UIColor.clearColor;

        CAShapeLayer *layer = [[CAShapeLayer alloc] init];

        CGMutablePathRef pathRef = CGPathCreateMutable();

        CGRect bounds = CGRectInset(cell.bounds, 0, 0);

        BOOL addLine = NO;

        if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)

        {
             CGPathAddRoundedRect(pathRef, nil, bounds, 0.0, 0.0);

        } else if (indexPath.row == 0)
        {

        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));

        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), 0.0);

        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), 0.0);

  CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
        addLine = YES;

        } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)

        {

        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));

        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), 0.0);
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), 0.0);
        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
        } else {
        CGPathMoveToPoint(pathRef, nil, bounds.origin.x, bounds.origin.y);
        CGPathAddLineToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
        CGPathMoveToPoint(pathRef, nil, CGRectGetMaxX(bounds), bounds.origin.y);
        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));

            addLine = YES;
        }
        layer.path = pathRef;
        CFRelease(pathRef);
        //set the border color

        layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
        layer.shadowRadius = 2;
        layer.shadowOpacity = 0.5;
        layer.strokeColor = [UIColor whiteColor].CGColor;
        layer.lineWidth = 1;
        if (addLine == YES)
        {
        CALayer *lineLayer = [[CALayer alloc] init];
        CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
        lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
        [layer addSublayer:lineLayer];
        }


        UIView *testView = [[UIView alloc] initWithFrame:bounds];
        [testView.layer insertSublayer:layer atIndex:0];
        testView.backgroundColor = [UIColor clearColor];
        cell.backgroundView = testView;

        testView.layer.shadowOffset = CGSizeMake(1, 1);
        testView.layer.shadowRadius = 2;
        testView.layer.shadowOpacity = 0.2;
        testView.layer.shadowColor = [UIColor blackColor].CGColor;
}
        NSLog(@"END row:%ld", (long)indexPath.row);
}

我必须像这张图片一样给出阴影:

enter image description here

3 个答案:

答案 0 :(得分:0)

  1. 您可以使用集合视图并创建自定义集合视图单元格(这样您就可以为XIB中的每个单元格添加边框)。
  2. 您可以在XIB中创建自定义表格视图单元格添加边框并删除表格视图换行符。但这不会给你与你在图片中完全相同的视图。最好实施我的第一个建议。
  3. 你说你想在选择单元格上绘制边框,然后你可以在XIB中添加透明颜色,并通过实现didSelectItemAtIndexPath方法在点击单元格中将它们着色。

答案 1 :(得分:0)

如果您只想将边框应用于表格单元格,则可以使用表格单元格内容视图并为其应用边框。

custom image

但是从你的图片中,我建议你有一个带边框的自定义图片,并使用表格部分,这样你就可以在各个部分之间留出所需的间距,你可以使用background image设置表格部分为UIView

执行此操作的一种方法是向表格单元格contentView添加自定义drawRect,并为UIView自定义- (void)drawRect: (CGRect)frame { //// Color Declarations UIColor* color = [UIColor colorWithRed: 0.967 green: 0.423 blue: 0.211 alpha: 1]; UIColor* shadowColor = [UIColor colorWithRed: 0.508 green: 0.508 blue: 0.508 alpha: 0.369]; //// Rectangle Drawing UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(CGRectGetMinX(frame) + 8.5, CGRectGetMinY(frame) + 12.5, 183, 131.5)]; [shadowColor setStroke]; rectanglePath.lineWidth = 2; [rectanglePath stroke]; //// Rectangle 2 Drawing UIBezierPath* rectangle2Path = [UIBezierPath bezierPathWithRect: CGRectMake(CGRectGetMinX(frame) + 12, CGRectGetMinY(frame) + 15, 176, 40)]; [color setFill]; [rectangle2Path fill]; }

{{1}}

这将给出结果:

enter image description here

确保为表格单元格设置正确的高度。还可以更改drawRectCode中的高度以满足您的需求!!!

答案 2 :(得分:0)

  1. 在tableView中:cellForRowAtIndexPath:

     [tableView setBackgroundColor:[UIColor clearColor]];
     [cell setBackgroundColor:[UIColor whiteColor]];
     [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
     [cell.layer setCornerRadius:10.0f];
     [cell.layer setMasksToBounds:YES];
     [cell.layer setBorderWidth:0.2f];
     [cell.layer setBorderColor:[UIColor blackColor].CGColor];
    

    您可以根据需要更改值,我尝试过它并且有效。

  2. 您可以将边框设置为图像。拍摄所需的图像,将其作为细胞的背景图像视图。

  3. 祝你好运:)