【发布时间】:2021-05-12 16:32:28
【问题描述】:
我创建了一个 UITableViewCell,它基本上是一张背后有阴影的卡片。我想让它成为用于所有其他自定义 UITableView 控制器的基类。但是,当我使用下面的代码时,阴影卡没有出现。这是为什么呢?
ShadowCardTableViewCell
class ShadowCardTableViewCell: UITableViewCell {
let borderView = UIView(frame: .zero)
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
changeToShadowedCard()
self.contentView.layoutIfNeeded()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func changeToShadowedCard() {
backgroundColor = .clear
contentView.addSubview(borderView)
borderView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
borderView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 10),
borderView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -10),
borderView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: -10),
borderView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: 10),
])
borderView.backgroundColor = .secondarySystemBackground
borderView.layer.cornerRadius = 7.5
borderView.addShadow(shadowColor: UIColor.label.cgColor, shadowOffset: CGSize(width: 0, height: 0), shadowOpacity: 0.3, shadowRadius: 4)
}
}
OtherTableViewCell
class OtherTableViewCell: ShadowCardTableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
【问题讨论】:
-
你的
addShadow(...)func 在做什么? -
而且...您是否通过
tableView.register(OtherTableViewCell.self, forCellReuseIdentifier: "otherCell")在代码中注册单元格?或者您是在 Storyboard Prototype 单元格中设置课程吗?
标签: ios swift xcode uitableview inheritance