繁体   English   中英

UITableView仅显示一个单元格

[英]UITableView showing only one cell

我有一个包含多个数据单元格的UITableView ,但加载时仅显示一个单元格。 为了测试,我在数组中添加了4个字符串。

class LoadingDataViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableViewDataLoading: UITableView!
    var dados = ["Vendas","Produtos","Pessoas","Animais"]

    override func viewDidLoad() {
        super.viewDidLoad()
        print("View did load")
        tableViewDataLoading.delegate = self
        tableViewDataLoading.dataSource = self
        tableViewDataLoading.layer.cornerRadius = 15.0
        tableViewDataLoading.layer.borderColor = UIColor.vorazColor.cgColor
        tableViewDataLoading.layer.borderWidth = 1.0
        tableViewDataLoading.clipsToBounds = true
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellLoadingData") as! CellDataLoading
        print("CELL: \(indexPath.row) - \(dados[indexPath.row])")
        cell.lblDado.text = dados[indexPath.row]
        cell.indicatorDado.startAnimating()
        return cell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: UITableViewAutomaticDimension))
        let label = UILabel(frame: headerView.frame)
        label.center = headerView.center
        headerView.backgroundColor = UIColor.vorazColor
        label.text = "Baixando dados..."
        label.textColor = UIColor.white
        headerView.addSubview(label)
        return headerView
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let buttonCancel = UIButton(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: UITableViewAutomaticDimension))
        buttonCancel.backgroundColor = UIColor.vorazColor
        buttonCancel.setTitle("Cancelar", for: .normal)
        buttonCancel.addTarget(self, action: #selector(cancelar), for: .touchUpInside)
        return buttonCancel
    }

    @objc func cancelar(_ sender : UIButton) {
        self.dismiss(animated: true, completion: {})
    }

}

class CellDataLoading : UITableViewCell {

    @IBOutlet weak var imgCheckDado: UIImageView!
    @IBOutlet weak var indicatorDado: UIActivityIndicatorView!
    @IBOutlet weak var lblDado: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

}

结果:

在此处输入图片说明

故事板:

在此处输入图片说明

观察:不显示标题视图的标签,并且此ViewController像弹出窗口一样显示。

这里有几个数字很重要。 从我的项目中查看屏幕截图: 我的表格视图

我给该表指定了明确的行高,在底部,我通过AutoLayoutw给了表一个固定的高度。

这些数字可能不适合您的项目,但是如果您调整单元格的高度,我认为您会看到更好的结果。

为您的UITableView设置高度约束,并按如下所示使用它:

@IBOutlet weak var tableHeightConstraint: NSLayoutConstraint!
override func viewDidLoad() {
    super.viewDidLoad()

    tableViewDataLoading.delegate = self
    tableViewDataLoading.dataSource = self
    tableViewDataLoading.layer.cornerRadius = 15.0
    tableViewDataLoading.layer.borderColor = UIColor.vorazColor.cgColor
    tableViewDataLoading.layer.borderWidth = 1.0
    tableViewDataLoading.clipsToBounds = true

    // For dynamic height of cells if you need that otherwise only write `heightForRowAtIndexPath`
    tableViewDataLoading.estimatedRowHeight = 88.0
    tableViewDataLoading.rowHeight = UITableViewAutomaticDimension
    tableViewDataLoading.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.new, context: nil)

}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    tableHeightConstraint.constant = tableViewDataLoading.contentSize.height
    UIView.animate(withDuration: 0.5) {
        self.updateConstraints()
        self.layoutIfNeeded()
    }

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    // For static height
    // return 40 // Here you can set your desired height 

    // For dynamic cell height 
    return UITableViewAutomaticDimension
}

尽管我比最近的项目(主要是观察表内容大小的观察者)更喜欢Dhaval D方法,但这是我在时间紧缩下所做的。

表格视图位于容器视图中。 容器视图具有高度限制。 表格视图在所有侧面都固定在“容器”视图上。 每当tableview数据更改时,我就打电话给

func updateTableSize() {

        self.tableView.layoutIfNeeded()
        var tableFrame = tableView.frame
        tableFrame.size.height = tableView.contentSize.height
        tableFrame.size.width = tableView.contentSize.width 
        tableView.frame = tableFrame
        heightConstraint.constant = tableFrame.size.height
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM