Ios 用自定义单元格填充UITableView

Ios 用自定义单元格填充UITableView,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试在我的应用程序中构建一个注册屏幕,我认为使用TableView是实现这一点的最佳方式 我的问题是我的自定义单元格没有显示它们应该显示的内容 这是我的SignUpTableView类: class SignUpTableView: UIView, UITableViewDelegate, UITableViewDataSource { let emailTextField: CustomTextField = { let v = CustomTe

我正在尝试在我的应用程序中构建一个注册屏幕,我认为使用
TableView
是实现这一点的最佳方式

我的问题是我的自定义单元格没有显示它们应该显示的内容

这是我的SignUpTableView类:

    class SignUpTableView: UIView, UITableViewDelegate, UITableViewDataSource {

    let emailTextField: CustomTextField = {
            let v = CustomTextField()
            v.borderActiveColor = .white
            v.borderInactiveColor = .white
            v.textColor = .white
            v.font = UIFont(name: "AvenirNext-Regular", size: 17)
            v.placeholder = "Email-Adresse"
            v.placeholderColor = .gray
            v.placeholderFontScale = 1
            v.clearButtonMode = .always
            v.minimumFontSize = 13
            v.borderStyle = .line
            v.autocapitalizationType = .none
            v.translatesAutoresizingMaskIntoConstraints = false
            return v
        }()


        let wishlistHandleTextField: CustomTextField = {
            let v = CustomTextField()
            v.borderActiveColor = .white
            v.borderInactiveColor = .white
            v.textColor = .white
            v.font = UIFont(name: "AvenirNext-Regular", size: 17)
            v.placeholder = "Wishlist-Handle"
            v.placeholderColor = .gray
            v.placeholderFontScale = 1
            v.clearButtonMode = .always
            v.minimumFontSize = 13
            v.borderStyle = .line
            v.autocapitalizationType = .none
            v.translatesAutoresizingMaskIntoConstraints = false
    //        v.leftView = handleLeftView
    //        v.leftViewMode = .always
            return v
        }()

        let anzeigeNameTextField: CustomTextField = {
            let v = CustomTextField()
            v.borderActiveColor = .white
            v.borderInactiveColor = .white
            v.textColor = .white
            v.font = UIFont(name: "AvenirNext-Regular", size: 17)
            v.placeholder = "Anzeigename: z.B. dein Vorname"
            v.placeholderColor = .gray
            v.placeholderFontScale = 1
            v.clearButtonMode = .always
            v.minimumFontSize = 13
            v.borderStyle = .line
            v.autocapitalizationType = .none
            v.translatesAutoresizingMaskIntoConstraints = false
            return v
        }()



        let passwordTextField: CustomTextField = {
            let v = CustomTextField()
            v.borderActiveColor = .white
            v.borderInactiveColor = .white
            v.textColor = .white
            v.font = UIFont(name: "AvenirNext-Regular", size: 17)
            v.placeholder = "Passwort"
            v.placeholderColor = .gray
            v.placeholderFontScale = 1
            v.minimumFontSize = 13
            v.borderStyle = .line
            v.addTarget(self, action: #selector(SignUpViewController.passwordTextFieldDidChange(_:)),for: .editingChanged)
            v.translatesAutoresizingMaskIntoConstraints = false
            return v
        }()

        let passwordWiederholenTextField: CustomTextField = {
            let v = CustomTextField()
            v.borderActiveColor = .white
            v.borderInactiveColor = .white
            v.textColor = .white
            v.font = UIFont(name: "AvenirNext-Regular", size: 17)
            v.placeholder = "Passwort wiederholen"
            v.placeholderColor = .gray
            v.placeholderFontScale = 1
            v.minimumFontSize = 13
            v.borderStyle = .line
            v.addTarget(self, action: #selector(SignUpViewController.passwordTextFieldDidChange(_:)),for: .editingChanged)
            v.translatesAutoresizingMaskIntoConstraints = false
            return v
        }()


        let signUpButton: TransitionButton = {
            let v = TransitionButton()
            v.translatesAutoresizingMaskIntoConstraints = false
            v.setTitle("Registrieren", for: .normal)
            v.titleLabel?.font = UIFont(name: "AvenirNext-DemiBold", size: 15)
            v.titleLabel?.textColor = .white
            v.setTitleColor(.white, for: .normal)
            v.backgroundColor = UIColor.darkGray
            v.layer.cornerRadius = 3
//            v.addTarget(self, action: #selector(signUpButtonTapped(_:)), for: .touchUpInside)
            return v
        }()

        let documentsLabel: UILabel = {
            let v = UILabel()
            v.text = "Durch Klicken auf 'Registrieren' akzeptiere ich die Nutzungbedingungnen und die Datenschutzrichtlinien."
            v.font = UIFont(name: "AvenirNext-Regular", size: 13)
            v.textColor = .lightGray
            v.textAlignment = .center
            v.numberOfLines = 0
            v.translatesAutoresizingMaskIntoConstraints = false
            return v
        }()

    var tableView = UITableView()


    override init(frame: CGRect) {
        super.init(frame: frame)

        tableView.backgroundColor = .clear

// disable didSelectAt
        self.tableView.allowsSelection = false


//        self.tableView.separatorStyle = .none

        self.tableView.register(SignUpTextfieldCell.self, forCellReuseIdentifier: SignUpTextfieldCell.reuseID)
        self.tableView.register(SignUpDocumentCell.self, forCellReuseIdentifier: SignUpDocumentCell.reuseID)
        self.tableView.register(SignUpButtonCell.self, forCellReuseIdentifier: SignUpButtonCell.reuseID)

        tableView.delegate = self
        tableView.dataSource = self


        tableView.translatesAutoresizingMaskIntoConstraints = false

        self.addSubview(tableView)

        tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - Table view data source

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 7
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 1st cell -> email textfield
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpTextfieldCell", for: indexPath) as! SignUpTextfieldCell
            cell.theTextField = self.emailTextField
            return cell
        // 2nd cell -> anzeigename
        }else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpTextfieldCell", for: indexPath) as! SignUpTextfieldCell
            cell.theTextField = self.anzeigeNameTextField
            return cell
        // 3rd cell -> Wishlist-Handle
        }else if indexPath.row == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpTextfieldCell", for: indexPath) as! SignUpTextfieldCell
            cell.theTextField = self.wishlistHandleTextField
            return cell
        // 4th cell -> passwort textfield
        }else if indexPath.row == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpTextfieldCell", for: indexPath) as! SignUpTextfieldCell
            cell.theTextField = self.passwordTextField
            return cell
        // 5th cell -> repeat password textfield
        }else if indexPath.row == 4 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpTextfieldCell", for: indexPath) as! SignUpTextfieldCell
            cell.theTextField = self.passwordWiederholenTextField
            return cell
        // 6th cell -> document label
        }else if indexPath.row == 5 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpDocumentCell", for: indexPath) as! SignUpDocumentCell
            cell.documentLabel = self.documentsLabel
            return cell
        }
        // last cell -> signUpButton
        let cell = tableView.dequeueReusableCell(withIdentifier: "SignUpButtonCell", for: indexPath) as! SignUpButtonCell
        cell.signUpButton = self.signUpButton
        return cell
    }

}
自定义单元格(这是我的
SignUpTextfieldCell
,其他单元格几乎相同)


当我将
SignUpTableView
的实例添加到我的
SignUpViewController
时,它会显示出来,但单元格为空。我尝试为第一个
单元格设置
backgroundColor
,效果很好。但是为什么我的
Textfields/Label/Button
没有显示?

我认为,您必须为单元格中的此元素设置约束,请尝试使用xib文件来构建此单元格? 如果你不喜欢xib,看看这个

我认为,您必须为单元格中的此元素设置控件,是否尝试使用xib文件构建此单元格? 如果你不喜欢xib,看看这个

尝试为
UITableView
添加高度,并尝试

yourTableView.rowHeight = 44
yourTableView.estimatedRowHeight = UITableView.automaticDimension

尝试为
UITableView
添加高度,并尝试

yourTableView.rowHeight = 44
yourTableView.estimatedRowHeight = UITableView.automaticDimension

我只需在每个
CustomCell
中创建所有
textFiedlds/label/button
就解决了这个问题。现在我有5个
单元格
,里面有一个
文本字段
,我想我可以通过上面的代码简化它,但它不起作用。

我只需在每个
自定义单元格
中创建所有
文本字段/标签/按钮
,就解决了这个问题。现在我有5个
单元格
,里面有一个
文本字段
,我想我可以通过上面的代码简化它,但它不起作用。

我做
约束
元素。看我更新的问题,我不约束元素。查看我的更新问题尝试在SignupTextField单元格中指定文本字段高度锚定值,在SignupTable View类中指定高度forrow尝试在SignupTextField单元格中指定文本字段高度锚定值,在SignupTable View类中指定高度forrow值,这会更改高度
,但仍无法显示内容,从而更改高度但内容仍然没有显示出来