用自定义单元格填充UITableView

时间:2020-01-27 22:28:59

标签: ios swift uitableview

我正在尝试在我的应用程序内部构建注册屏幕,尽管使用TableView是实现此目的的最佳方法。

我的问题是我的自定义单元格未显示其应有的内容。

这是我的 SignUpTableView-Class:

    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,其他几乎相同)

    class SignUpTextfieldCell: UITableViewCell {

    public static let reuseID = "SignUpTextfieldCell"

    var theTextField: UITextField = {
        let v = UITextField()
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

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

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.backgroundColor = .clear

        setupViews()
    }

    func setupViews(){
        contentView.addSubview(theTextField)

        theTextField.topAnchor.constraint(equalTo: topAnchor).isActive = true
        theTextField.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        theTextField.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        theTextField.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    }
}

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

3 个答案:

答案 0 :(得分:0)

我认为,必须在单元格中为此元素设置禁忌素,方法是尝试使用xib文件构建此单元格? 如果你不喜欢xib,看看这个 programatically custom Cell

答案 1 :(得分:0)

尝试为UITableView添加高度,然后尝试

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

答案 2 :(得分:0)

我通过简单地在每个textFiedlds/label/button中创建所有CustomCell来解决了这个问题。现在我有5个cells,里面有一个textfield,我想可以通过上面的代码简化它,但是它不起作用。

相关问题