首页 > 解决方案 > 用自定义单元格填充 UITableView

问题描述

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

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

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

    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为第一个设置 acell并且有效。但是为什么我Textfields/Label/Button没有被显示?

标签: iosswiftuitableview

解决方案


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


推荐阅读