Xcode报错如题
重现错误情景:
在使用自动布局完成tableViewCell的自适应时出现该问题。
如何使用AutoLayout实现tableViewCell的自适应:
1.使用AutoLayout对cell的子视图设置在contentView中自上而下完整的垂直约束。
添加约束时的要点:a.一定要保持完整的自上至下的约束;b.为放置imageView的UIView类型的控件添加约束时将高度设置为0,它会自动根据子视图的内容和大小进行高度自适应。当然也要给其中使用代码添加的子视图使用代码进行约束布局。
2.给tableViewCell设置预估高度。
3.对xib cell文件进行注册复用即可自适应高度
编程中永远相信你不是第一个遇到该问题的人。
cocoaChina上也有人提问了该问题,我是链接,但请不要点我,但是回答很。。。
http://www.cocoachina.com/bbs/read.php?tid=253807
看到这种答案挺尴尬的,唯一的好处就是知道有难兄难弟在:

当然该问题之下也有正常的回答,但是也都没有指出问题所在。
回答的就像:电脑死机怎么办?重装系统呗。我电脑word打不开了怎么办?重装系统呗。。。
Stack overflow上的回答更为全面合理链接如下:点击我可直接访问
http://stackoverflow.com/questions/24217195/why-do-i-get-must-translate-autoresizing-mask-into-constraints-to-have-sethost
该问题的回答非常详尽,可是没一个是我的情况,所以我一遍遍的改约束改到脑袋快要爆炸。可以访问网站的建议直接去访问该问题,回答很详细。(当然也有直接丢锅给Xcode的**)
不想看的我就简单概括一下该问题:当UIView和UITableViewCell两个类型的实例发生交叉感染时就出现该问题。
会出现该问题的情况包括:
1.使用UIView类型的实例当作UITableViewCell使用
2.将UITableViewCell添加到了UIView类型的实例上
3.设置属性里将UIView和UITableViewCell两者类型弄混
解决:细心认真的设置好属性与对应视图的类型
问题:
I have the following code in a swift UITableViewController, but i get
a "Must translate autoresizing mask into constraints to have
_setHostsLayoutEngine:YES exception at the 2nd line. I didnt change any
settings in the interface builder (so Autolayout and size classes are
both checked).
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
let cell:TodoItemCell = tableView.dequeueReusableCellWithIdentifier("TodoItemCell", forIndexPath: indexPath) as TodoItemCell
let row = indexPath.row
cell.titleLabel.text = self.todoItems![row].title
self.callback!(row)
// Configure the cell...
return cell
}
What am i doing wrong?
回答:
1.Not sure how much this helps, but I see the same error if I try to
place a cell directly in a UIView rather than in a table. This worked
fine with Xcode 5 so I suspect it is a bug with XCode6/ios8. In the mean
time, debug into that method and look at your tableView. Make sure it
actually has an instance of the cell you are trying to retrieve.
Update:Filed a bug report with Apple and I can confirm that this is an ios8 issue. Unfortunately it is still there as of beta-5, hopefully they will have it fixed before the final release.
2.This isNOTa bug, you simply cannot use aUIViewas cell for table view, you must make sure the top level view in the nib file is aUITableViewCell.
3.In my case, I was loading a nib file with a UITableViewCell contained in
a UIView. After disabling autolayout in the nib file, I haven't got the
exception again.
4.I also ran into this issue and it was due to a UITableViewCell subclass being used in the view hierarchy outside of a UITableView as noted in the other answers. In this case I had limited time, so I couldn't move the functionality present in that subclass into a UIView subclass. The workaround I came up with was to just create a UIView instance in my view hierarchy where the cell was supposed to be and transplant the view hierarchy of the cell instance into it, and remove the cell itself from the displayed view hierarchy. So long as I kept the views I moved wired to the various properties of the cell instance, everything keeps working fine.
It's a bit hacky, but it will keep things running until the time to refactor is available.
网友评论