Ios 如何创建从另一个UITableViewCell继承的UITableViewCell?
我创建了一个UITableViewCell,它基本上是一张有阴影的卡片。我想让它成为所有其他自定义UITableView控制器使用的基类。然而,当我使用下面的代码时,阴影卡没有出现。为什么会这样 ShadowCardTableViewCellIos 如何创建从另一个UITableViewCell继承的UITableViewCell?,ios,swift,xcode,uitableview,inheritance,Ios,Swift,Xcode,Uitableview,Inheritance,我创建了一个UITableViewCell,它基本上是一张有阴影的卡片。我想让它成为所有其他自定义UITableView控制器使用的基类。然而,当我使用下面的代码时,阴影卡没有出现。为什么会这样 ShadowCardTableViewCell class ShadowCardTableViewCell: UITableViewCell { let borderView = UIView(frame: .zero) override init(style: UITabl
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)
}
}
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)
}
}
OtherTableViewCell
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)
}
}
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的基类。 您可以做的是为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.setShadow()
首先,不能将UITableViewCell作为UiViewController的基类。 您可以做的是为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.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
}
}
结果:
您还没有显示您的
borderView.addShadow(…)
函数正在做什么,但这对我来说很好:
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
}
}
结果:
你的
addShadow(…)
func在做什么?还有。。。是否通过tableView.register(OtherTableViewCell.self,forCellReuseIdentifier:“otherCell”)
在代码中注册单元格?或者您正在故事板原型单元中设置类?您的addShadow(…)
func在做什么?以及。。。是否通过tableView.register(OtherTableViewCell.self,forCellReuseIdentifier:“otherCell”)
在代码中注册单元格?或者您是在故事板原型单元中设置类?