ios - 如何创建包含 UITextField 子类的自定义 TextCell 和 TextRow?
问题描述
我正在使用Eureka Forms和SearchTextField。
在我的表单中,我希望有一些与 (Eureka) 内置的非常相似的东西TextRow
。不同之处在于此自定义TextRow
将使用 aSearchTextField
而不是常规的UITextField
。
我首先认为我可以复制所有与类相关的代码TextRow
并TextCell
稍微重命名这些类。但是,我发现代码_FieldRow
很难理解,而且我不知道应该复制什么代码。
然后我看到我可以设置cellProvider
属性以提供自定义单元格:
<<< TextRow() {
$0.cellProvider = CellProvider<TextCell>(nibName: "NewCustomCell", bundle: .main)
$0.title = "TextRow"
}
所以我想我只需要一个xib文件。我试图找到 Eureka 使用的 xib 文件,TextCell
以便我可以复制它并稍微编辑它,但我在目录下找不到单个 xib 文件Pods/Eureka
。然后我尝试制作自己的xib文件。
添加表格视图单元格后,我将其类设置为,TextCell
因为我需要一个CellProvider<TextCell>
. 然后我尝试将文本字段和标签连接到IBOutlet
s in _FieldCell
(这是我应该做的,对吗?)但插座只是没有出现。我将课程更改为_FieldCell
并且他们正确显示,但是我不能将带有 a 的笔尖传递_FieldCell
到 a 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() {
$0.title = "Search"
guard let tf = $0.cell.textField as? SearchTextField else {
return
}
tf.filterStrings(["lorem", "ipsum", "dolor", "sit", "amet"])
}
推荐阅读
- python - 即使在 jupyter notebook 中使用 plt.close(),如何防止 matplotlib 显示图形
- node.js - 有没有办法只得到消息作者的名字
- sql - 跨重复 ID 查找查询中的最新项目
- amazon-web-services - 身份验证的 API 速率限制
- flutter - 颤振:下拉项目以编程方式取消选择问题
- java - PIT 测试说 Minion 由于 TIMED_OUT 异常退出
- python - 未找到任何参数的“详细信息”反向。尝试了 1 种模式:['product/(?P
[-a-zA-Z0-9_]+)$'] - ansible - 在 ansible 上使用 jinja 循环在变量中设置接口名称
- asp.net-core - 使用带有 .Net Core WebApi 的 Blazor 进行 Windows 身份验证
- php - Codeigniter - 使用 dompdf 生成的 pdf 不符合 html 格式