如何制作一个继承自另一个 UITableViewCell 的 UITableViewCell?
How to make a UITableViewCell that inherits from another UITableViewCell?
我创建了一个 UITableViewCell,它基本上是一张后面有阴影的卡片。我想让它成为用于所有其他自定义 UITableView 控制器的基础 class。但是,当我使用下面的代码时,阴影卡没有出现。这是为什么?
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)
}
}
首先,您不能将 UITableViewCell 作为 UiViewController 的基础 class。
您可以做的是为您的 UITableViewCell 提供一个扩展,大致如下:
extension UITableViewCell {
func setShadow() {
layer.shadowColor = UIColor.lightGray.cgColor
layer.shadowOpacity = 0.25
layer.borderColor = UIColor.separator.cgColor
layer.borderWidth = 1.0
layer.cornerRadius = 10
layer.shadowRadius = 1.0
layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
}
}
注册一个cell并初始化后,像这样调用它:
cell.setShadow()
你没有展示你的 borderView.addShadow(...)
func 在做什么,但这对我来说工作正常:
class ShadowCardTableViewCell: UITableViewCell {
let borderView = UIView(frame: .zero)
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
changeToShadowedCard()
// this line shouldn't be needed
//self.contentView.layoutIfNeeded()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// this line needed if setting the cell class as a Prototype in Storyboard
changeToShadowedCard()
}
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
// not sure what you are doing here
//borderView.addShadow(shadowColor: UIColor.label.cgColor, shadowOffset: CGSize(width: 0, height: 0), shadowOpacity: 0.3, shadowRadius: 4)
// but this seems to work fine
borderView.layer.shadowColor = UIColor.label.cgColor
borderView.layer.shadowOffset = CGSize(width: 0, height: 0)
borderView.layer.shadowOpacity = 0.3
borderView.layer.shadowRadius = 4
}
}
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)
}
}
class SubCellVC: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// if you have not set your cell as a Storyboard Prototype
// register it here
tableView.register(OtherTableViewCell.self, forCellReuseIdentifier: "otherCell")
tableView.rowHeight = 60
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "otherCell", for: indexPath)
return cell
}
}
结果:
我创建了一个 UITableViewCell,它基本上是一张后面有阴影的卡片。我想让它成为用于所有其他自定义 UITableView 控制器的基础 class。但是,当我使用下面的代码时,阴影卡没有出现。这是为什么?
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)
}
}
首先,您不能将 UITableViewCell 作为 UiViewController 的基础 class。 您可以做的是为您的 UITableViewCell 提供一个扩展,大致如下:
extension UITableViewCell {
func setShadow() {
layer.shadowColor = UIColor.lightGray.cgColor
layer.shadowOpacity = 0.25
layer.borderColor = UIColor.separator.cgColor
layer.borderWidth = 1.0
layer.cornerRadius = 10
layer.shadowRadius = 1.0
layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
}
}
注册一个cell并初始化后,像这样调用它:
cell.setShadow()
你没有展示你的 borderView.addShadow(...)
func 在做什么,但这对我来说工作正常:
class ShadowCardTableViewCell: UITableViewCell {
let borderView = UIView(frame: .zero)
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
changeToShadowedCard()
// this line shouldn't be needed
//self.contentView.layoutIfNeeded()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// this line needed if setting the cell class as a Prototype in Storyboard
changeToShadowedCard()
}
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
// not sure what you are doing here
//borderView.addShadow(shadowColor: UIColor.label.cgColor, shadowOffset: CGSize(width: 0, height: 0), shadowOpacity: 0.3, shadowRadius: 4)
// but this seems to work fine
borderView.layer.shadowColor = UIColor.label.cgColor
borderView.layer.shadowOffset = CGSize(width: 0, height: 0)
borderView.layer.shadowOpacity = 0.3
borderView.layer.shadowRadius = 4
}
}
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)
}
}
class SubCellVC: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// if you have not set your cell as a Storyboard Prototype
// register it here
tableView.register(OtherTableViewCell.self, forCellReuseIdentifier: "otherCell")
tableView.rowHeight = 60
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "otherCell", for: indexPath)
return cell
}
}
结果: