如何创建自定义 TextCell 和 TextRow,其中包含 UITextField 的子类?
How can I create a custom TextCell and TextRow that has a subclass of UITextField in it?
我正在使用 Eureka Forms and SearchTextField。
在我的表单中,我想要一些与 (Eureka) 内置的非常相似的东西 TextRow
。不同之处在于此自定义 TextRow
将使用 SearchTextField
而不是常规 UITextField
.
我首先想到的是我可以复制与 TextRow
和 TextCell
相关的所有代码,并稍微重命名 classes。但是,我发现 _FieldRow
中的代码很难理解,我不知道应该复制什么代码。
然后我看到我可以设置 cellProvider
属性 来提供自定义单元格:
<<< TextRow() {
[=10=].cellProvider = CellProvider<TextCell>(nibName: "NewCustomCell", bundle: .main)
[=10=].title = "TextRow"
}
所以我想我只需要一个 xib 文件。我试图找到 Eureka 用于其 TextCell
的 xib 文件,以便我可以复制它并稍微编辑它,但我在 Pods/Eureka
目录下找不到单个 xib 文件。然后我尝试制作自己的 xib 文件。
添加 table 视图单元格后,我将其 class 设置为 TextCell
,因为我需要一个 CellProvider<TextCell>
。然后,我尝试将文本字段和标签连接到 _FieldCell
中的 IBOutlet
(这是我应该做的,对吧?)但是插座没有出现。我将 class 更改为 _FieldCell
,它们正确显示,但我无法将带有 _FieldCell
的笔尖传递到 CellProvider<TextCell>
,可以吗?
重申我的目标:创建一个表单单元格,将 SearchTextField
作为文本字段,并且在其他所有方面的行为与 TextCell
相同。
您需要使用自定义行和单元格...类似这样
class _SearchTextFieldCell<T>: _FieldCell<T> where T: Equatable, T: InputTypeInitiable {
required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.textField.removeFromSuperview()
let searchTextField = SearchTextField()
searchTextField.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(searchTextField)
self.textField = searchTextField
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class SearchTextCell: _SearchTextFieldCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
open override func setup() {
super.setup()
}
}
class _SearchTextRow: FieldRow<SearchTextCell> {
public required init(tag: String?) {
super.init(tag: tag)
}
}
final class SearchTextRow: _SearchTextRow, RowType {
required public init(tag: String?) {
super.init(tag: tag)
}
}
然后您可以将自定义行与
一起使用
<<< SearchTextRow() {
[=11=].title = "Search"
guard let tf = [=11=].cell.textField as? SearchTextField else {
return
}
tf.filterStrings(["lorem", "ipsum", "dolor", "sit", "amet"])
}
我正在使用 Eureka Forms and SearchTextField。
在我的表单中,我想要一些与 (Eureka) 内置的非常相似的东西 TextRow
。不同之处在于此自定义 TextRow
将使用 SearchTextField
而不是常规 UITextField
.
我首先想到的是我可以复制与 TextRow
和 TextCell
相关的所有代码,并稍微重命名 classes。但是,我发现 _FieldRow
中的代码很难理解,我不知道应该复制什么代码。
然后我看到我可以设置 cellProvider
属性 来提供自定义单元格:
<<< TextRow() {
[=10=].cellProvider = CellProvider<TextCell>(nibName: "NewCustomCell", bundle: .main)
[=10=].title = "TextRow"
}
所以我想我只需要一个 xib 文件。我试图找到 Eureka 用于其 TextCell
的 xib 文件,以便我可以复制它并稍微编辑它,但我在 Pods/Eureka
目录下找不到单个 xib 文件。然后我尝试制作自己的 xib 文件。
添加 table 视图单元格后,我将其 class 设置为 TextCell
,因为我需要一个 CellProvider<TextCell>
。然后,我尝试将文本字段和标签连接到 _FieldCell
中的 IBOutlet
(这是我应该做的,对吧?)但是插座没有出现。我将 class 更改为 _FieldCell
,它们正确显示,但我无法将带有 _FieldCell
的笔尖传递到 CellProvider<TextCell>
,可以吗?
重申我的目标:创建一个表单单元格,将 SearchTextField
作为文本字段,并且在其他所有方面的行为与 TextCell
相同。
您需要使用自定义行和单元格...类似这样
class _SearchTextFieldCell<T>: _FieldCell<T> where T: Equatable, T: InputTypeInitiable {
required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.textField.removeFromSuperview()
let searchTextField = SearchTextField()
searchTextField.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(searchTextField)
self.textField = searchTextField
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class SearchTextCell: _SearchTextFieldCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
open override func setup() {
super.setup()
}
}
class _SearchTextRow: FieldRow<SearchTextCell> {
public required init(tag: String?) {
super.init(tag: tag)
}
}
final class SearchTextRow: _SearchTextRow, RowType {
required public init(tag: String?) {
super.init(tag: tag)
}
}
然后您可以将自定义行与
一起使用<<< SearchTextRow() {
[=11=].title = "Search"
guard let tf = [=11=].cell.textField as? SearchTextField else {
return
}
tf.filterStrings(["lorem", "ipsum", "dolor", "sit", "amet"])
}