简体   繁体   English

UITableView单元格下拉阴影问题

[英]UITableView Cell Drop Shadow Issue

I am using the following code to give the last cell in my tableview a drop shadow 我正在使用以下代码为TableView中的最后一个单元格添加阴影

if (indexPath.row == myLastCellNum) {
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.layer.shadowOpacity = 0.5;
    cell.layer.shadowOffset = CGSizeMake(0, 1);
}

else {
    cell.layer.shadowColor = [[UIColor clearColor] CGColor];
    cell.layer.shadowOpacity = 0.0;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
}

You can see in the picture bellow that the bottom of the last cell has a drop shadow. 您可以在下面的图片波纹中看到,最后一个单元格的底部有一个阴影。 That is what I wanted. 那就是我想要的。

在此处输入图片说明

The issue is that whenever I scroll the tableview up or down, the tableview looks like the image bellow 问题是,每当我上下滚动tableview时,tableview看起来就像下面的图像 在此处输入图片说明

Here, you see that the separator between the last and second to last cell also has a shadow. 在这里,您看到最后一个和倒数第二个单元格之间的分隔符也有阴影。 This shadow is more pronounced on the device (the pictures are from the simulator). 该阴影在设备上更为明显(图片来自模拟器)。

So my question is, how do I get rid of this extra shadow between the last and second to last cell? 所以我的问题是,如何消除最后一个和倒数第二个单元之间的多余阴影?

If you are using this in cellForRowAtIndexPath:, then how about: 如果您在cellForRowAtIndexPath:中使用此方法,那么如何:

cell.layer.shadowColor = [[UIColor clearColor] CGColor];
cell.layer.shadowOpacity = 0.0;
cell.layer.shadowOffset = CGSizeMake(0, 0);

if (indexPath.row == myLastCellNum) {
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.layer.shadowOpacity = 0.5;
    cell.layer.shadowOffset = CGSizeMake(0, 1);
}

That way you are making sure that the cell has no shadow to begin with (for cell reuse), then only giving it a shadow if the if statement is entered. 这样,您可以确保该单元格没有开始的阴影(用于单元格重用),然后仅在输入if语句时才为其提供阴影。

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

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