YSh*_*hin 26 uitableview autolayout swift xcode6
我正在尝试使用Swift UITaleViewCell在UITableViewXCode6中实现动态高度.
我通过图形化设置约束来布置我的单元格(屏幕截图来自XCode5,因为XCode6上有NDA).我还将BodyLabel的换行符属性设置为"自动换行",并将行号设置为"0"以允许多行.

现在,如果我只是在tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?)方法内部设置单元格的内容,那么我就可以正确获得动态高度行为.
但是,由于我按照在线提供的教程(特别是这个),我添加了另一种方法来确定细胞的高度tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!).在我关注的教程中,它告诉我添加cell.layoutIfNeeded(),所以我也添加了.
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
var cell = tableView!.dequeueReusableCellWithIdentifier(kCellIdentifier) as GroupFeedCell
// Configure the cell
if let frc = self.fetchedResultsController {
let feed = frc.objectAtIndexPath(indexPath) as GroupFeed
cell.titleLabel.text = feed.name
if let message = feed.message {
cell.bodyLabel.text = message
}
}
// Layout the cell
cell.layoutIfNeeded()
// Get the height
var height : CGFloat = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
return height
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行程序时,虽然表视图仍然正确显示单元格的动态高度,但它显示如下错误:
2014-07-27 13:59:22.599 FBGroups[4631:979424] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x17809e780 H:[UILabel:0x14fd12f50'Body Label Contents...']-(8)-| (Names: '|':UITableViewCellContentView:0x178185d70 )>",
"<NSLayoutConstraint:0x17809e7d0 H:|-(8)-[UILabel:0x14fd12f50'Body Label Contents...'] (Names: '|':UITableViewCellContentView:0x178185d70 )>",
"<NSLayoutConstraint:0x17809ea50 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x178185d70(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x17809e780 H:[UILabel:0x14fd12f50'Body Label Contents...']-(8)-| (Names: '|':UITableViewCellContentView:0x178185d70 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚可能出错的地方,花了很多时间在上面,我发现每当我删除cell.layoutIfNeeded()方法时,约束错误就会消失.
似乎在相互冲突的约束中,UIView-Encapsulated-Layout-Width不是我添加的那个,除此之外,所有约束对我来说都是无辜的.我试图搜索可能产生UIView-Encapsulated-Layout-Width约束的东西,但我无法在我的情况下得到令人满意的解释.我想知道这个错误消息的原因是什么以及如何解决此问题.
另外,有人可以解释cell.layoutIfNeeded()在计算单元格高度时调用方法的目的是什么,何时需要?大多数覆盖动态高度的教程都UITableViewCell使用了这种方法,虽然我的程序在没有调用方法的情况下仍能正确显示单元格,每当我尝试使用该方法时,它都会导致异常.
冲突的约束可能是因为前两个约束将标签的宽度定义为比单元格的宽度小 16pts,但第三个约束希望UIView-Encapsulated-Layout-Width使标签具有不同的大小。
您可以尝试降低内容压缩阻力/拥抱属性的值,以允许标签忽略其固有尺寸。
| 归档时间: |
|
| 查看次数: |
5761 次 |
| 最近记录: |