【发布时间】:2020-04-14 18:46:53
【问题描述】:
我正在尝试实现一些我不确定是否可行的事情。我有一个 UITableViewCell 的 xib,其中有一个 UIButton,并在情节提要中分配了自定义类,即 MainButtonSuperClass。在UITableViewCell.swift 出口连接如下,
@IBOutlet weak var button: MainButtonSuperClass!
许多不同的控制器正在配置这个TableViewCell,但按钮的 UI 对于所有在子类中定义的控制器都会有所不同,即
final class MainButtonSubClass: MainButtonSuperClass {
override func configure(){//different style of button }
}
我怎样才能做到这一点?到目前为止,我已经尝试过,
let button: MainButtonSuperClass = MainButtonSubClass()
button.configure()
cell.button = button
但这并没有选择风格
编辑:
//tableview cell
class TableViewCell: UITableViewCell {
@IBOutlet weak var button: MainButtonSuperClass!
}
//Controller A wants default style of superclass (MainButtonSuperClass)
func configureButton(buttonCell: TableViewCell) {
buttonCell.button.configure() //works
}
//Controller B wants different style of subclass (MainButtonSubClass)
func configureButton(buttonCell: TableViewCell) {
let button: MainButtonSuperClass = MainButtonSubClass()
button.configure()
cell.button = button // doesn't work or adopts the style
}
//Custom button's classes
final class MainButtonSuperClass: UIButton {
override func configure(){//styled button }
}
final class MainButtonSubClass: MainButtonSuperClass {
override func configure(){//different style of button }
}
【问题讨论】:
-
您是否尝试根据正在使用的控制器更改按钮的外观?
-
是的,不同的控制器正在配置这个
TableViewCell,但它们都有不同的按钮样式。假设TableViewControllerFoo想要单元格蓝色按钮TableViewController酒吧想要带有黑色按钮的单元格。我的UIButton的超类是蓝色的,子类是黑色的,IBOutlet是超类的例子,但是其他控制器如何将其更改为子类 -
@jay 我已经用更多代码更新了我的问题,如果它可以清除它。谢谢
标签: swift uitableview subclass iboutlet