我有一個相當標準的UITableView通過自定義單元填充。那個單元現在只是一個圖像和一個標籤。在我的生活中,我無法調整它自己。無法讓UITableViewAutomaticDimension正常工作
當我包含UITableViewAutomaticDimension時,除了不正確的佈局外,我無法填充我的數據。
沒有UITableViewAutomaticDimension,數據顯示正常。
我使用SnapKit處理約束是和流星/ SwiftDDP來處理數據,但有另一種的UITableView中,這似乎是正確
工作的ViewController
class CommentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var commentTable:UITableView!
var comments:MeteorCollection<Comment>!
init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
comments = MeteorCollection<Comment>(name: "comments")
createView()
Meteor.subscribe("postsComments", params: [["postId": self.source!.id!]]) {}
}
func createView() {
let contentTableView = UITableView(frame: content.frame)
content.addSubview(contentTableView)
contentTableView.backgroundColor = UIColor.clearColor()
self.commentTable = contentTableView
contentTableView.delegate = self
contentTableView.dataSource = self
contentTableView.snp_makeConstraints { (make) -> Void in
make.top.equalTo(content)
make.left.equalTo(content)
make.right.equalTo(content)
make.height.equalTo(content).inset(65)
}
contentTableView.rowHeight = UITableViewAutomaticDimension
contentTableView.estimatedRowHeight = 350
}
}
項目評論TableViewDelegate.swift
import UIKit
extension CommentViewController {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.comments.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(CommentTableViewCell.reuseIdentifier, forIndexPath: indexPath) as UITableViewCell
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
if let card = cell as? CommentTableViewCell {
let item = self.comments.sorted[indexPath.row]
card.populate(item)
}
return CommentTableViewCell()
}
func reloadTableView() {
self.commentTable.reloadData()
}
}
在使用UITableViewAutomaticDimension 不
我亂碼混亂的一個例子使用UITableViewAutomaticDimension
請將contentTableView.estimatedRowHeight = 1個//估計值,如果小於正常工作 –