【问题标题】:UITableViewCell with automatic height and Autolayout具有自动高度和自动布局的 UITableViewCell
【发布时间】:2021-10-12 17:02:54
【问题描述】:

我正在创建一个单元格具有自动高度的 UITableView。每个单元格都有 3 个使用自动布局的 UILabel:

我认为我已将所有约束设置得很好,但是我遇到了一些奇怪的事情,并且事件标签的某些单元格的高度超出了预期:

有些行的高度可以,但有些行不行,Event lavel 的高度可能会更低。

override func viewDidLoad() {
    super.viewDidLoad()

    // First load table nib
    let bundle = Bundle(for: type(of: self))

    // Register table cell class from nib
    let cellNib = UINib(nibName: "OnlineEventsCell", bundle: bundle)
    tableView.register(cellNib, forCellReuseIdentifier: "onlineEventsCell")
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    
    self.tableView.estimatedRowHeight = 59.0; // set to whatever "average" cell height is
    self.tableView.rowHeight = UITableView.automaticDimension;
    self.navigationItem.title = NSLocalizedString("ActiveEvents", comment: "")
    
    searchBar.delegate=self
    self.refreshControl?.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)

    loadViewData(reloading: false, eventName: nil)
    self.tableView.reloadData()
    
    
}

【问题讨论】:

  • 您是否尝试在viewDidLoad() 中设置estimatedRowHeightrowHeight

标签: ios swift xcode uitableview autolayout


【解决方案1】:

您可以使用this image.中所示的单元格约束

并将其添加到您的代码中:

extension MainViewController: UITableViewDelegate, UITableViewDataSource {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initTableView()
        
    }

    func initTableView() {
        
        self.tableViewController.delegate = self
        self.tableViewController.dataSource = self
        
        self.tableViewController.register(UINib(nibName: "HotsTableViewCell", bundle: nil), forCellReuseIdentifier: "HotsTableViewCell")
        
        self.tableViewController.backgroundColor = .white
        self.tableViewController.rowHeight = UITableView.automaticDimension
        self.tableViewController.bounces = false
        self.tableViewController.estimatedRowHeight = 600
        self.tableViewController.reloadData()

    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return arrData.count

    }
        
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if arrData.count > 0 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "HotsTableViewCell", for: indexPath) as! HotsTableViewCell
            cell.selectionStyle = .none
            cell.configuration(example: arrData[indexPath.row])
            return cell
        }
        
        return UITableViewCell()

    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

}

结果将如this image.所示

【讨论】:

    猜你喜欢
    • 2014-01-03
    • 2017-04-07
    • 2014-07-03
    • 2021-05-10
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    相关资源
    最近更新 更多