Ios UITableCell未正确调整大小
在开发我的第一个iPhone应用程序时,我在尝试使用“自动”调整表格单元格大小时遇到了一个问题。当我加载应用程序时,前几个单元格总是很大的,大约是它应该的6倍大。然后,当我向下和向上滚动时,它们将重置为应该的高度。(如图所示,难以解释) (前两幅图片) 到目前为止,我已经尝试了一些教程和stackoverflow答案,但我无法在这里发布,因为我只能发布2个链接,其中大多数只涉及添加UITableViewAutomaticDimension属性 我在故事板中创建了该单元(包含4个标签,所有标签的#line设置为0和3个图像(参见第三个图像)),并为其指定了一个标识符,然后为其创建了一个自定义类来更改标签的文本 调整单元格大小,如下所示:Ios UITableCell未正确调整大小,ios,swift,uitableview,Ios,Swift,Uitableview,在开发我的第一个iPhone应用程序时,我在尝试使用“自动”调整表格单元格大小时遇到了一个问题。当我加载应用程序时,前几个单元格总是很大的,大约是它应该的6倍大。然后,当我向下和向上滚动时,它们将重置为应该的高度。(如图所示,难以解释) (前两幅图片) 到目前为止,我已经尝试了一些教程和stackoverflow答案,但我无法在这里发布,因为我只能发布2个链接,其中大多数只涉及添加UITableViewAutomaticDimension属性 我在故事板中创建了该单元(包含4个标签,所有标签的#
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 104
使用下面这句话似乎可以解决大多数设备上的问题,但它仍然存在于iPhone 6 plus上(第四幅图):
绘制单元格:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:TableCell? = tableView.dequeueReusableCellWithIdentifier("tableCell") as? TableCell
if cell == nil {
cell = TableCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "tableCell")
}
cell?.lblObituaryName.text = obituaryArray[indexPath.row].name
cell?.lblObituaryBirth.text = obituaryArray[indexPath.row].dateOfBirth
cell?.lblObituaryDeath.text = obituaryArray[indexPath.row].dateOfDeath
cell?.lblObituaryPlace.text = obituaryArray[indexPath.row].city
cell?.setNeedsUpdateConstraints()
cell?.updateConstraintsIfNeeded()
return cell!
}
故事板中的单元设置是第五个也是最后一个图像
单元约束:
Table cell is 104.0 high
Main title label:
Trailing space to: Superview = 0
Leading space to: Superview = 0
Top space to: Superview = 0
Bottom space to: Label1 = 6
Bottom space to: Label1Image = 1
Label 1:
Trailing space to: Superview = 0
Leading space to: Label1Image = 5
Bottom space to: Label2 = 6
Top space to: Main Label = 6
Label 1 Image:
Leading space to: Superview = 0
Width = 25
Height = 25
Trailing space to: Label1 = 5
Bottom space to: Label2Image = -3
Top space to: Main Label = 1
Label 2:
Trailing space to: Superview = 0
Leading space to: Label2Image = 5
Bottom space to: Label3 = 6
Top space to: Label2 = 6
Label 2 Image:
Leading space to: Superview = 0
Width = 25
Height = 25
Trailing space to: Label2 = 5
Bottom space to: Label3Image = -3
Top space to: Label1Image = -3
Label 3:
Trailing space to: Superview = 0
Bottom space to: Superview = -1
Leading space to: Label3Image = 5
Top space to: Label2 = 6
Label 3 Image:
Leading space to: Superview = 0
Width = 25
Height = 25
Trailing space to: Label3 = 5
Top space to: Label2Image = -3
通过将约束优先级更改为999,修复了以下错误。但仍在寻找上述问题的答案。
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:0x7a0affb0 UILabel:0x7a079ea0'Mw. J.H. Zwierenga'.top == UITableViewCellContentView:0x7a074170.topMargin>",
"<NSLayoutConstraint:0x7a0b00a0 V:[UILabel:0x7a079ea0'Mw. J.H. Zwierenga']-(6)-[UILabel:0x7a0394f0'21 May 2015']>",
"<NSLayoutConstraint:0x7a0b0190 V:[UILabel:0x7a0394f0'21 May 2015']-(6)-[UILabel:0x7a0a1f20'1 December 1935']>",
"<NSLayoutConstraint:0x7a0b0280 UITableViewCellContentView:0x7a074170.bottomMargin == UILabel:0x7a094e10'Oudeschild'.bottom - 1>",
"<NSLayoutConstraint:0x7a0b02b0 V:[UILabel:0x7a0a1f20'1 December 1935']-(6)-[UILabel:0x7a094e10'Oudeschild']>",
"<NSLayoutConstraint:0x7a0b2fe0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7a074170(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a0b02b0 V:[UILabel:0x7a0a1f20'1 December 1935']-(6)-[UILabel:0x7a094e10'Oudeschild']>
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.
无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
如果需要更多信息,请告诉我。试试这个
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 104;
}
如果将“附件视图”设置为“无”,则“所有”都可以正常工作这确实可以防止单元格像我显示的屏幕截图中那样异常巨大,但是标签没有拆分为多行(),您是否设置了label.numberOfLines=0;?通过故事板是的,只是在程序上也尝试过,但还是断掉了尾巴。当我将“换行符”设置为Word Wrap时,它也不会显示第二行;对于动态标签大小或增加标签高度,以便为文本创建足够的空间。相同的结果是,名称的结尾仍然会在结尾处被截断。由于某种原因,它不会移到下一行:(将一些垂直和水平约束设置为999确实修复了错误。不幸的是,使用layoutIfNeeded并没有改变任何东西。也许我用错了?尝试在整个单元格和标签上调用它,导致cellForRowAtIndexPath方法中出现问题。你能在github上用你的单元格创建测试项目吗?我修复了它们,然后重新编写。)我尝试了很多crunch=)但没有任何帮助。但如果将“附件视图”单元属性设置为“无”,则“自动布局”将正常工作。快速解决方案用自定义UIImageView替换附件视图。我当时开始这个项目时一定是做错了什么。那我就定一个尺寸吧。感谢您花时间尝试我的项目。
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 104;
}