首页 > 解决方案 > 将 UI 快速添加到单元格

问题描述

我有一个应用程序,我正在使用动态 UI,我很乐意调整要添加到单元格的 UI 类型。现在的问题是当添加 UI 时,它是不可编辑的,例如,如果添加了 uitextfield,我无法添加任何输入或编辑它,如果添加了日期选择器,则无法编辑它。欢迎任何帮助。下面是我的代码的附件

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellModel = elements[indexPath.row]
        let cellIdentifier = cellModel.unique_id
//            cellModel.type.rawValue
        let customCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier!, for: indexPath) as! CustomElementCell

        customCell.configure(withModel: cellModel)

        return customCell as! UITableViewCell
    }

// 我的单元格

func configureUI(type: String, placeholder: String) {

        let type = TypeView.typeCell(type: type, placeholder: placeholder)
        switch type {

        case .text:
            let tf = DefaultTextField()
            tf.placeholder = placeholder
            self.contentView.addSubview(tf)
            log("THE LOG test")
        case .embeddedphoto:
            let tf = DefaultTextField()
            tf.placeholder = placeholder
            tf.keyboardType = .numberPad
            log("THE LOG embeddedphoto")
        case .formattednumeric:
            let tf = DefaultTextField()
            tf.placeholder = placeholder
            log("THE LOG formattednumeric")
        case .datetime:
            let dp = UIDatePicker()
            self.contentView.addSubview(dp)
            log("THE LOG datetime")
        }

    }

标签: swift

解决方案


1-您应该为每个动态类型创建一个单元格并注册它

2-对于您当前的实现,您需要为添加的元素添加约束并清除单元格以避免出队问题


推荐阅读