Aviav Sabag
Aviav Sabag

Reputation: 81

Auto-layout problem (?) with custom tableView cells | Swift

Hi guys :) I'm kinda having a problem with auto-layout my custom cells, im having 5 custom cells which all are loaded but the thing is they override each other no matter what I do, Im trying to find the problem of it but I dont seem to find it :\ would love to get a hand with that please. Btw : Im not using any storyboards.

Here's my tableView's code:

import UIKit
import PanModal


class FilterTableViewController: UITableViewController, PanModalPresentable {
    var panScrollable: UIScrollView? {
        return tableView
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
//        registerTableViewCells()
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
//        tableView.frame = view.bounds
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    
    
    // MARK: - View Configurations
    
    func setupTableView() {
        
        
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
        tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true

        
        tableView.separatorStyle = .singleLine
        tableView.isScrollEnabled = false // We don't want it to scroll
        tableView.allowsSelection = false

        
        tableView.estimatedRowHeight = 160
        tableView.rowHeight = UITableView.automaticDimension
        tableView.rowHeight = 80
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)

    }
    
    private func registerTableViewCells() {
        let textFieldCell = UINib(nibName: "byActivityCell",
                                  bundle: nil)
        self.tableView.register(textFieldCell,
                                forCellReuseIdentifier: "cell")
    }
    
    
    
    // MARK: - UITableViewDataSource
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        switch indexPath.row {
        case 0:
            let byActivityCell = UINib(nibName: "byActivityCell",bundle: nil)
            self.tableView.register(byActivityCell,forCellReuseIdentifier: "byActivityCell")
            return tableView.dequeueReusableCell(withIdentifier: "byActivityCell", for: indexPath) as! byActivityCell
            
        case 1:
            let byTypeCell = UINib(nibName: "ByType",bundle: nil)
            self.tableView.register(byTypeCell,forCellReuseIdentifier: "byTypeCell")
            return tableView.dequeueReusableCell(withIdentifier: "byTypeCell", for: indexPath) as! ByType
            
        case 2:
            let byHashtagsCell = UINib(nibName: "ByHashtags",bundle: nil)
            self.tableView.register(byHashtagsCell,forCellReuseIdentifier: "byHashtagsCell")
            return tableView.dequeueReusableCell(withIdentifier: "byHashtagsCell", for: indexPath) as! ByHashtags
            
        case 3:
            let byDatesCell = UINib(nibName: "DatesCell",bundle: nil)
            self.tableView.register(byDatesCell,forCellReuseIdentifier: "byDatesCell")
            return tableView.dequeueReusableCell(withIdentifier: "byDatesCell", for: indexPath) as! DatesCell
            
            
        case 4:
            let byAlbumCell = UINib(nibName: "AlbumCell",bundle: nil)
            self.tableView.register(byAlbumCell,forCellReuseIdentifier: "byAlbumCell")
            return tableView.dequeueReusableCell(withIdentifier: "byAlbumCell", for: indexPath) as! AlbumCell
            
        default:
            return UITableViewCell()
        }
        
    }
    
    
    
    
    
    
    // MARK: - footer Methods:
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return getfooterView()
    }

    func getfooterView() -> UIView
    {
        let Header = UIView(frame: CGRect(x: 0, y: 0, width: Double(self.tableView.frame.size.width), height: 45))
        Header.backgroundColor = UIColor(named: "#2AF8AC")

        let button = UIButton()
        button.frame = CGRect(x: 0, y: 0, width: Header.frame.size.width-10, height: Header.frame.size.height)
        
        
        
        button.backgroundColor = .systemBlue
        button.layer.cornerRadius = 12
        button.layer.masksToBounds = true

        button.setTitle("Apply Filters", for: .normal)
        button.setTitleColor(.white, for: .normal)

        //       button.addTarget(self, action: nil, for: UIControl.Event.touchUpInside)

        Header.addSubview(button)
        Header.bringSubviewToFront(button)
        return Header
    }

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 45
    }

    
    
}

Here's how it looks like: Link.

Upvotes: 0

Views: 826

Answers (1)

Arik Segal
Arik Segal

Reputation: 3031

  1. Remove this line:

    tableView.rowHeight = 80

  2. Follow this guideline: The trick to get Auto Layout working on a UITableViewCell is to ensure that you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints. Then, the intrinsic height of the subviews will be used to dictate the height of each cell.

(quoted from https://www.raywenderlich.com/8549-self-sizing-table-view-cells)

Upvotes: 1

Related Questions