问题
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 is NOT a bug, you simply cannot use a UIView as cell for table view, you must make sure the top level view in the nib file is a UITableViewCell.
回答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.
回答5:
PEOPLE, THIS IS NOT A BUG. As never said down below:
"This is NOT bug, you simply cannot use a UIView as cell for table view, you must make sure the top level view in the nib file is a UITableViewCell."
I have tested it and it works.
You need to create the Xib file and delete the top-level view that it has. After, you drag and drop a UITableViewCell into the nib. This way, this problem will not happen.
回答6:
Unselecting 'use auto layout' option from the .xib's properties resolved my issue. - Posting what I did as this may help others.
Thank you.
回答7:
In my case i was using a UITableViewCell in a UIView to create a custom cell
then I deleted the cell from the UIView and then added the all the contents in the UIView directly and that solved my problem.
Hope it helps you too. All the best
回答8:
In My case I had copied and pasted the cell, but constraints were missing that resulted in this error. if you are copying the cell into another xib file, ensure constraints are also copied or create them again
回答9:
In my case the UITableViewCell parent was the main view instead of the UITableView. In this case you just have to put the cell inside the tableview.
I think this could be a common issue if you drag the cell into the view without taking care of the correct hierarchy.
来源:https://stackoverflow.com/questions/24217195/why-do-i-get-must-translate-autoresizing-mask-into-constraints-to-have-sethost