在其单元格中有阴影的TableView

问题描述 投票:0回答:1

你好,我有一个普通的TableView,我想更新它,使它在TableView的每个单元格之间有空格,也从每个侧面(上,下,左,右)。

正如你可以看到这里,我有一个普通的TableView,我想把它更新为在TableView的每个单元格之间有空格,也可以从每个侧面(上下、左右)。enter image description here

ios swift tableview
1个回答
4
投票

首先,你需要创建一个新的UITableViewCell,它里面有一个视图contentView。你需要让你添加的这个视图比你的视图小,并且填满,这样你就可以在每个tableviewCell之间创建间隙。

像这样。

enter image description here

你可以创建约束条件,使视图变小,并适合在单元格内,就像这样。

enter image description here

然后你就可以使用这个扩展来创建视图中的阴影。

https:/stackoverflow.coma407201228792385。

代码片段是这样的。

extension UIView {

  // OUTPUT 1
  func dropShadow(scale: Bool = true) {
    layer.masksToBounds = false
    layer.shadowColor = UIColor.black.cgColor
    layer.shadowOpacity = 0.5
    layer.shadowOffset = CGSize(width: -1, height: 1)
    layer.shadowRadius = 1

    layer.shadowPath = UIBezierPath(rect: bounds).cgPath
    layer.shouldRasterize = true
    layer.rasterizationScale = scale ? UIScreen.main.scale : 1
  }

}

然后你只需要为你的视图创建一个引用 然后在你的配置单元格方法中调用

view.dropShadow()

结果会像这样。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.