【问题标题】:Auto-layout problem (?) with custom tableView cells | Swift自定义 tableView 单元格的自动布局问题 (?) |迅速
【发布时间】:2021-05-01 03:04:58
【问题描述】:

大家好 :) 我的自定义单元格的自动布局有点问题,我有 5 个自定义单元格都已加载,但问题是无论我做什么,它们都会相互覆盖,我试图找到它的问题,但我似乎没有找到它:\ 很想得到帮助。 顺便说一句:我没有使用任何故事板。

这是我的 tableView 的代码:

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
    }

    
    
}

如下所示:Link.

【问题讨论】:

    标签: ios swift autolayout tableview custom-cell


    【解决方案1】:
    1. 删除这一行:

      tableView.rowHeight = 80

    2. 遵循以下准则: 让自动布局在 UITableViewCell 上工作的技巧是确保您有约束将每个子视图固定在所有边上——也就是说,每个子视图应该有前导、顶部、尾随和底部约束。然后,子视图的固有高度将用于指示每个单元格的高度。

    (引自https://www.raywenderlich.com/8549-self-sizing-table-view-cells

    【讨论】:

    • 好的。我已经编辑了我的答案并添加了第二部分
    • 很棒的教程兄弟 :) 会读到那篇文章,也许他们会为我找到答案。
    猜你喜欢
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2015-08-15
    相关资源
    最近更新 更多