简体   繁体   English

滚动后UITableViewCell阴影消失

[英]UITableViewCell shadow disappears after scroll

I have subclassed UITableViewCell class to add shadow below my cell. 我已经将UITableViewCell类子类化为在我的单元格下面添加阴影。 The shadow is added correctly, when TableView appears on screen. 当TableView出现在屏幕上时,阴影会正确添加。 But, when I scroll tableview down, and cell with shadow hides above the screen, the shadow disappears. 但是,当我向下滚动tableview,并且阴影的单元格隐藏在屏幕上方时,阴影消失。

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.shouldAddShadow) {
        self.layer.shadowOpacity = 0.5;
        self.layer.shadowRadius = 1.5;
        self.layer.shadowOffset = CGSizeMake(0, 3);
        self.layer.shadowColor = [[[UIColor appDarkDividerColor] colorWithAlphaComponent:0.9] CGColor];
        [self setClipsToBounds:NO];
        [self.layer setMasksToBounds:NO];
        CGRect shadowFrame = self.layer.bounds;
        CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
        self.layer.shadowPath = shadowPath;
    }
}

forgot to mention, that i have tableview with static cells; 忘了提,我有静态细胞的tableview; so prepareForReuse isn't called. 所以不会调用prepareForReuse。 I have outlets for my cells, so that i've also tried to set the shadow to my cell in scrollViewDidScroll: method. 我有我的单元格的插座,所以我也尝试在scrollViewDidScroll:方法中将阴影设置到我的单元格。 Even this din't help me 即便如此,这对我没有帮助

I just encountered this problem. 我刚遇到这个问题。 Finally I find the way to make it work. 最后,我找到了让它发挥作用的方法。

It doesn't disappear(removed), it was just been hidden. 它不会消失(删除),它只是被隐藏了。

There we use a property zPosition of cell's layer. 我们使用了单元格层的属性zPosition

From Apple docs : 来自Apple文档

The default value of this property is 0. Changing the value of this property changes the the front-to-back ordering of layers onscreen. 此属性的默认值为0.更改此属性的值会更改屏幕上图层的前后排序。 This can affect the visibility of layers whose frame rectangles overlap. 这会影响框架矩形重叠的图层的可见性。

The value of this property is measured in points. 此属性的值以磅为单位。

The default value is 0 . 默认值为0 This leads top cell hides the bottom cell (say shadow). 这导致顶部细胞隐藏底部细胞(比如阴影)。 It means if you set shadow for a view to make it show in both sides and the margin before two cell is zero, only bottom shadow of the top cell will show up, the top shadow of the bottom shadow will be hidden. 这意味着如果为视图设置阴影以使其显示在两侧并且两个单元格之前的边距为零,则仅显示顶部单元格的底部阴影,底部阴影的顶部阴影将被隐藏。

When the cell goes out of the screen and then back, though the zPosition of each cell is still 0 , for those cells, bottom cell hides top cell now. 当细胞离开屏幕然后返回时,尽管每个细胞的zPosition仍为0 ,但对于那些细胞,底部细胞现在隐藏顶部细胞。 The hide direction is opposite to your scroll direction. 隐藏方向与滚动方向相反。 This is exactly the situation you met. 这正是你遇到的情况。

So, 所以,

cell.layer.zPosition = <#value you want#>

For example, I want to show shadow of the siblings, I can set zPosition of this cell's layer to -1 , then shadow of both side will appear. 例如,我想显示兄弟姐妹的阴影,我可以将此单元格的图层的zPosition设置为-1 ,然后将出现两边的阴影。

zPosition of a layer decide which cell can show in the front, and which shows in the back. zPosition一个层决定哪个单元格可以在前面显示,哪个单元格在后面显示。 Like z-index in CSS. 像CSS中的z-index一样。

So the solution is change the zPosition property to make it work as you expected. 因此,解决方案是更改zPosition属性以使其按预期工作。


In addition, you should not set cell's clipsToBounds to YES . 此外,您不应将单元格的clipsToBounds设置为YES (default value is NO) (默认值为NO)

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

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