Fill UITableView with Custom Cells

Related searches

I am trying to build a sign up screen inside my app and I though using a TableView is the best way to realize this.

My problem is that my custom cells are not displaying what they should..

This is my 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
    }

}

Custom-Cells (this is my SignUpTextfieldCell, the others are pretty much the same)

    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
    }
}

When I add an instance of SignUpTableView to my SignUpViewController it get's displayed but with empty cells. I tried setting a backgroundColor for the first cell and that works. But why are my Textfields/Label/Button not being displayed?

i think, you must set contrains for this elements in cell, meaby try use xib file to build this cell ? if you don't like xib, look at this programatically custom Cell

How To Create a Custom UITableViewCell | by Petey, UITableViewCell (a.k.a. cell) is one of the most used views when developing It's very useful and is easily customizable to any developer's liking. but there's still a bit of work to be done before we can start populating values� Here we will see how to add custom images to table view cells along with text in iOS swift with example. To use iOS table view in our applications we need to add UITableView reference. Create iOS TableView with Custom Cell App in Swift. To create a new project in iOS open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below.

Try to add a height for UITableView and also try

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

How to populate tableView custom cell in Swift 3?, Create a subclass from UITableViewCell f.e. NewsCell . connect your outlets @ IBOutlet weak var headline: UILabel! @IBOutlet weak var� UITableView with Custom Cell (Swift 4 + Xcode 9.0) - Duration: 11:13. PlanetVeracity 55,419 views. 11:13. Swift - Build Your First App in 30 minutes - For Beginners - Music Player - Duration

I solved the problem by simply creating all the textFiedlds/label/button in each CustomCell. Now I have 5 cells with a textfield inside and I thought I could simplify that by the code above but it's not working.

UITableView, Today we're gonna dive into creating custom table view cells using all code baby! Hope you Duration: 15:41 Posted: Feb 23, 2018 Fill in “SimpleTableCell” for the class name and select “UITableViewCell” for the “Subclass of” option. Click “Next”, save the file in the SimpleTable project folder and click “Create” to continue. Xcode should create two files named “SimpleTableCell.h” and “SimpleTableCell.m” in the Project Navigator.

Configuring the Cells for Your Table, In your tableView(_:cellForRowAt:) method, configure the content of your cell using For custom cells, you need to define a UITableViewCell subclass to access� Custom cells for static table view are done in the exact same way as custom cells for dynamic table views. One needs to register the nib and then use the cellForRowAt method to use the custom cell. The biggest difference when using a static table view is that you need to do it through Interface builder, and it needs to use a UITableViewController.

Building a Custom Cell, Name it HabitTableViewCell and make it a subclass of UITableViewCell. Select the option "Also create XIB file". Then click Next. This will create two new files. One� I am facing an issue with UITableView. I want to dynamically fill its cells with data fetched from a remote database, so it takes some times before the data arrived. Here is the code: class MailBoxViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView!

Add a UITableViewCell as the root of your xib file and any other visual components you want. Create a cocoa touch class file with class name MyCustomCell as a subclass of UITableViewCell. Set the custom class and reuse identifier for your custom table view cell. Open the assistant editor and ctrl+drag to create outlets for your visual components.

Comments
  • Try to give theTextField heightAnchor in SignUpTextfieldCell and heightForRow in SignUpTableView class
  • I do constrain the elements. Look at my updated question
  • that changes the height but the content is still not being displayed