代码之家  ›  专栏  ›  技术社区  ›  Sweeper

如何创建一个自定义的textcell和textrow,其中包含一个子类uitextfield?

  •  0
  • Sweeper  · 技术社区  · 7 年前

    我正在使用 Eureka Forms SearchTextField .

    在我的表单中,我想要一些与(eureka)内置的非常类似的东西 TextRow . 区别在于这个习俗 特克特罗 将使用 SearchTextField 而不是普通的 UITextField .

    我首先想到我可以复制所有与 特克特罗 TextCell 并稍微重命名类。但是,我发现代码在 _FieldRow 很难理解,我不知道应该复制什么代码。

    然后我看到我可以设置 cellProvider 属性以提供自定义单元格:

    <<< TextRow() {
        $0.cellProvider = CellProvider<TextCell>(nibName: "NewCustomCell", bundle: .main)
        $0.title = "TextRow"
    }
    

    所以我想我只需要一个XIB文件。我试着找到Eureka用于 文本单元 这样我可以复制和编辑它,但在 Pods/Eureka 目录。然后我试着做我自己的XIB文件。

    在添加表视图单元后,我将其类设置为 文本单元 因为我需要一个 CellProvider<TextCell> . 然后我尝试将文本字段和标签连接到 IBOutlet S在 _FieldCell (这是我应该做的,对吗?)但这些渠道根本就不存在。我把班级改为 γ场电池 他们出现得很好,但我不能用 γ场电池 变成一个 手机供应商<textcell> 我能吗?

    重申我的目标:创建一个具有 搜索文本字段 作为文本字段,其行为与 文本单元 其他的一切。

    1 回复  |  直到 7 年前
        1
  •  1
  •   koropok    7 年前

    您需要使用自定义行和单元格。像这样的东西

    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"])
    }
    
    推荐文章