ios - TableView 自定义单元格设计问题

标签 ios swift uitableview uiview

我正在尝试创建一个具有圆角和阴影的自定义表格单元格。最终目标是创建“卡片”外观和感觉。我的 Storyboard设置如下。我在单元格的内容 View 上创建了一个单独的 View ,以便我可以操纵该 View 以获得我想要的效果。问题是效果似乎只影响 View 的顶部。底角不是圆角,您还可以看到边框围绕着整个 View 。我也包含了自定义单元类文件。关于如何使 View 的其余部分具有圆角和边框的任何想法?

class EventTableCell: UITableViewCell {

@IBOutlet weak var collectionView: UIView!
@IBOutlet weak var address: UILabel!
@IBOutlet weak var statusImage: UIImageView!
@IBOutlet weak var customerName: UILabel!
@IBOutlet weak var customerTime: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    //collectionView.backgroundColor = UIColor.gray
    collectionView.clipsToBounds = false
    collectionView.layer.cornerRadius = 10
    collectionView.layer.borderWidth = 1
    collectionView.layer.borderColor = UIColor.gray.cgColor
    collectionView.layer.shadowColor = UIColor.lightGray.cgColor
    collectionView.layer.shadowOpacity = 1
    collectionView.layer.shadowOffset = CGSize.zero
    collectionView.layer.shadowRadius = 3
   }
 }

主 Storyboard中 UiTableview 的屏幕截图:

Screen Shot

输出的屏幕截图:

Screen Shot

最佳答案

使用这个方法:

extension UIView {

func addShadow(offset: CGSize, color: UIColor, radius: CGFloat, opacity: Float) {
    layer.masksToBounds = false
    layer.shadowOffset = offset
    layer.shadowColor = color.cgColor
    layer.shadowRadius = radius
    layer.shadowOpacity = opacity

    let backgroundCGColor = backgroundColor?.cgColor
    backgroundColor = nil
    layer.backgroundColor =  backgroundCGColor
   }
}

像这样调用这个方法:

view.addShadow(offset: CGSize(width: 5, height: 5), color: .lightGray, radius: 5, opacity: 0.5)

可以在tableCell的'awakeFromNib'方法中调用该方法。

注意:还要给表格行适当的高度。

关于ios - TableView 自定义单元格设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53625779/

相关文章:

ios - 第二个 View Controller 中的空 UIPickerView

ios - 如何为警报确定操作按钮提供背景颜色以及如何快速为警报中的标题提供顶部空间

swift - 我想使用 SnapKit 将顶部 UIView 放置在 UITableView 之上

ios - tableView.dequeueReusableCellWithIdentifier VS tableView.cellForRowAtIndexPath

ios - Swift Firebase 为 UIActivityViewController 附加图像

使用 FCM 的 IOS 数据通知

iphone - 这个警告是什么意思 : "setting the first responder view of the collection view but we don' t know its type (cell/header/footer)"

objective-c - 如何从 Swift 调用 Objective-C 代码?

ios - 如何调用嵌入了UICollectionView的UITableViewCell的indexPath.row

ios - Parse-Server iOS SDK - 创建返回 PFQuery 结果的函数