【发布时间】:2015-03-06 17:21:44
【问题描述】:
第一次加载表格视图时,所有可见单元格都是估计的行高。当我向下滚动时,单元格会自动调整大小,当我向上滚动时,最初估计的单元格会自动调整大小。
一旦单元格被自动调整大小,它们似乎永远不会回到估计的RowHeight。
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
self.tableView.estimatedRowHeight = 80.0
self.tableView.rowHeight = UITableViewAutomaticDimension
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
和
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as CustomTableViewCell
// Configure the cell...
let restaurant = restaurants[indexPath.row]
cell.namelabel.text = restaurant.name
cell.locationlabel.text = restaurant.location
cell.typelabel.text = restaurant.type
cell.thumbnailImageView.image = UIImage(named: restaurant.image)
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width / 2
cell.thumbnailImageView.clipsToBounds = true
cell.accessoryType = restaurant.isVisited ? .Checkmark : .None
return cell
}
关于如何让单元格最初自动调整大小的想法?
更新:从 Xcode 7 beta 6 开始,这不再是问题
【问题讨论】:
-
还要注意,我的 AutoLayout 约束设置正确(三重检查)
-
我遇到了类似的问题。我可以通过设置控制 tableCell 高度的约束来解决它而无需重新加载表格部分 - 在我的情况下,将内容视图 bottomMarginTop 固定到 textView 底部的约束优先级为 1000。即使这会导致不可满足的约束错误,最终结果是表格视图单元格正确显示。
-
这在 Xcode 9.2 中仍然是一个问题
标签: ios uitableview swift uikit