如何在tableviewcell swift 3添加阴影?

时间:2017-04-18 03:42:27

标签: ios swift uitableview swift3

我想在tableviewcell上添加4个部分的阴影(1个tableView和4个tableViewCell),其中我想在3个边(左,右,下)添加阴影的最后一个单元格,但是除了我想要的最后一个单元格之外的另一个单元格在2侧(左,右)添加阴影。我用单例和扩展名UIView编写这段代码。 这是最后一个单元格:

 func dropShadowAtBottom() {

    self.layer.masksToBounds = false
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOpacity = 0.2
    self.layer.opacity = 0.3
    self.layer.shadowOffset = CGSize(width: 0, height: 2)
    self.layer.shadowRadius = 2
    self.clipsToBounds = false

}

这是除了最后一个单元格之外的另一个单元格:

func dropShadowAtLeftAndRight() {

    self.layer.masksToBounds = false
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOpacity = 0.5
    self.layer.shadowOffset = CGSize(width: 0, height: 0)
    self.layer.shadowRadius = 2
    let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 4);  // inset top/bottom
    self.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath

    self.layer.rasterizationScale = UIScreen.main.scale
    self.layer.shouldRasterize = true

}

我在cellForRow中调用此代码:

if(indexPath.row == currentOrderHistory.listOrderItems.count - 1){
            cell.bodyView.dropShadowAtBottomOnly()

        }else{
            cell.bodyView.dropShadowAtLeftAndRight()
        }
        return cell

现在这段代码工作但仍然不完美。细胞之间有空白区域。

picture

我只想让它们连接起来。

2 个答案:

答案 0 :(得分:0)

您是否尝试从IB中删除分隔符?

enter image description here

分隔符设置为

或其他方式,将分隔符颜色设置为半透明

import sys
ALPHABET= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ']


def main(plaintext, key):
     cypher = generate_cypher(key)
     plaintext = sys.argv[1]
     e_code = encode_message(plaintext, cypher)
     mes s= decode_message(e_code, cypher)

def generate_cypher(key):
    cipher = []
    for i in range(len(ALPHABET)):
        cipher.append(ALPHABET[i+key%27])
    print(cipher)
    return(cipher)


def encode_message(plaintext,cipher):     
    key = int(sys.argv[2])
    en_code=''
    for c in plaintext:
        if c in cipher:
            en_code+=cipher[(cipher.index(c)+key)%(len(cipher))]
    print("Encoded: ", en_code)
    return en_code

def decode_message(e_code,cipher):
    key = int(sys.argv[2])
    message = []
    for i in range(len(e_code)):
        message.append(cipher[i-key%27])

    mess=''.join(message)
    print("Decoded: ", mess)
    return mess

答案 1 :(得分:0)

我认为您在此行的dropShadowAtLeftAndRight()中遇到了问题

let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 4); 

在我的情况下,它会返回rect看起来像(0.0, 4.0, 375.0, 36.0)。在顶部4px是白色空间,底部也是4px白色空间

所以你的左右阴影将从4开始,高度为36。

要解决此问题,您需要像这样设置

let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 0);