UITableViewAutomaticDimension不能工作,直到滚动
当第一次加载表视图时,所有可见的单元格是estimatedRowHeight。 当我向下滚动时,单元格会自动resize,当向后滚动最初估计的单元格时,会自动resize。
一旦细胞自动大小,他们似乎再也没有回到估计的高度。
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 7testing版6这不再是一个问题
我遇到了同样的问题,发现附件 cell.accessoryType
与这个自动resize,当它不是None
,所以它看起来像XCode目前的错误。
但是@Blankarsch提到,调用reloadSections(..)
有助于解决这个问题,如果你需要一个配件。
在你的表被加载后,只需调用“reloadSections”:
self.tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, self.tableView.numberOfSections())), withRowAnimation: .None)
或者在Swift 3中:
let range = Range(uncheckedBounds: (lower: 0, upper: self.tableView.numberOfSections)) self.tableView.reloadSections(IndexSet(integersIn: range), with: .none)
只是像这样覆盖estimatedHeightForRowAtIndexPath
(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; }
并在CustomTableViewCell视图中检查您的autoLayout约束。
我认为这里的答案副作用较less。 在tableView:cellForRowAtIndexPath:
特别添加cell.layoutIfNeeded()
tableView:cellForRowAtIndexPath:
我也在做@ nosov认为是好的做法,但还没有testing是否需要一起完成。
只要确保您的标签没有在Interface Builder中设置的显式内容大小。 它们必须是Automatic(自动),如下面的截图所示,Automatic Row高度才能正常工作,无需重新加载任何部分。
如果您基于单元格本身(而不是单元格内容视图)设置约束,则表格无法获得适当的大小。 所以,要解决这个问题,您的约束必须设置为内容视图。
但是,当您的单元格同时支持带/不带辅助视图的configuration时,这是个问题。 在这种情况下,内容视图根据附件视图被resize,结果可能不是用户所期望的。 因此,在这种情况下,一个解决scheme是设置两个约束,一个是单元格,另一个是低优先级的单元格内容视图。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
在函数中设置单元格的首选内容,以便UITableViewAutomaticDimension完美地工作。
问题是由于单元格内容被加载到其他一些代理函数中造成的,因此您可以看到单元格自动调整为所需的大小。
确保在tableView:cellForRowAt:
初始化你的单元格。 当我在tableView:willDisplay:forRowAt:
设置单元格内容时,即使使用Xcode 8,我也遇到过这个问题tableView:willDisplay:forRowAt:
对我来说,当我使用willDisplay单元格来设置我的标签的文本时,我只有这个问题
如果我在cellForRowAt索引path中设置我的标签的文本,一切都很好
对我来说,解决的是@ [David Hernandez]答案的组合。
从上面删除select,并在单元格layoutSubviews我设置preferredMaxLayoutWidth为(更改30到您想要的左右间距)
-(void) layoutSubviews { [super layoutSubviews]; self.textDescriptionLabel.preferredMaxLayoutWidth = self.frame.size.width - 30; }