【发布时间】:2014-11-10 09:07:45
【问题描述】:
正如你在图片中看到的,中间的单元格有一个占用两行的 UILabel,但文本实际上是单行。似乎如果文本只需要几个字符就可以创建一个新行,iOS 假设它已经有 2 行。这很奇怪。
这就是我创建标签的方式:
self.titleLabel.lineBreakMode = .ByTruncatingTail
self.titleLabel.numberOfLines = 0
self.titleLabel.textAlignment = .Left
约束设置一次:
self.titleLabel.autoPinEdgeToSuperviewEdge(.Top)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Leading)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Trailing)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Bottom)
奇怪的是,当表格滚动时,奇数单元格消失并再次滚动回它具有正常高度。滚动后:
任何想法有什么问题吗?我正在使用 swift、xcode6.1 和 iOS8.1
TableViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(CityTableViewCell.self, forCellReuseIdentifier:"cell")
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 52
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell: CityTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as? CityTableViewCell {
// Configure the cell for this indexPath
cell.updateFonts()
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
if indexPath.row == 1 {
cell.titleLabel.text = "Welcome to city 17, Mr. Gordon F."
} else {
cell.titleLabel.text = "Lamar!!!"
}
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
return cell
}
return UITableViewCell();
}
【问题讨论】:
-
请发布您的 cellForRowAtIndexPath 代码。你是否也实现了 willDisplayCell: 方法?
-
@ZeMoon 查看我的更新。我没有实现 willDisplayCell
-
尝试将行 cell.setNeedsUpdateConstraints() 和 cell.updateConstraintsIfNeeded() 移动到 willDisplayCell: 方法
-
@ZeMoon 已经解决了。感谢您的帮助!
-
@Pintouch 我知道这个解决方案,但是表格视图会随着数据的重新加载而闪烁。接受的答案是更好的解决方案,您应该尝试一下。
标签: ios uitableview swift uilabel