我有一个带有多个数据单元格的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像弹出窗口一样显示。
答案 0 :(得分:0)
我给了这个表一个明确的行高,在底部,我通过AutoLayoutw给了这个表一个固定的高度。
这些数字可能不适合您的项目,但是如果您调整单元格的高度,我认为您会看到更好的结果。
答案 1 :(得分:0)
为您的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
}
答案 2 :(得分:0)
虽然我比最近的项目更喜欢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
}