49

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?

Emwee
  • 545
  • 1
  • 5
  • 7

13 Answers13

55

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.

pbuchheit
  • 1,371
  • 1
  • 20
  • 47
  • 4
    This is not fixed in the Xcode 6 GM. My issue was that a xib mistakenly had a view with a custom class descending from UITableViewCell in the middle of a view hierarchy. – Jesse Rusak Sep 09 '14 at 20:48
  • Me too, tried on Xcode 6 GM, still there. I'm seeing this error if my .xib file has a TableViewCell embedded in another UITableViewCell. – KBog Sep 16 '14 at 07:36
  • 1
    I confirm this issue on Xcode 6.0.1. Is there any workaround? For example placing a single row `UITableView` inside `UIView` to host the `UITableViewCell`? – mxb Sep 18 '14 at 15:16
  • 1
    The only way that "worked" was disabling Auto-Layout. Which isn't a solution. So I'm back to Xcode 5 until it gets fixed. – KBog Sep 18 '14 at 19:00
  • 2
    Still an issue in Xcode 6.3 and iOS 8.3. This was indeed the issue in my case - I had a UITableViewCell (for its convenience when using the Right Detail style) directly in a UIView. I replaced it with two labels. – Craig Otis Feb 23 '15 at 14:01
  • This issue is so annoying, is happen with UICollectionView as well, the autoresizing mask made by translateautoresizingmaskintoconstrainst is causing me this issue. – Darklex Apr 24 '15 at 16:54
  • 1
    If Apple have not addressed this issue by now, then apparently they just do not see it as a priority. My suggestion for anyone who runs across this is to go and file a bug report like I did. Mabey if enough people bug them about it they will take a look. – pbuchheit Aug 06 '15 at 12:56
34

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.

neevek
  • 11,760
  • 8
  • 55
  • 73
  • 2
    A lot of comments mention disabling Auto-Layout as well. This is _not_ something that should be considered a valid option, in an age of size classes and multiple screen resolutions. +1 to @neevek for correct hierarchy recommendation. – Daniel J Aug 29 '15 at 20:24
  • I actually agree, this is most likely the way this was intended to work. However, trying to use a table cell to layout some content is a natural response to a lot of users. It is on Apple to either change the compiler to prevent this mistake, or at the very least DOCUMENT the fact that a cell cannot be used separately from a table. – pbuchheit Sep 09 '15 at 17:25
  • The only solution that worked for me. Thank you very much. Reading above answers, I was scared thinking I may need to make a lot of changes.... – Ritesh Kumar Gupta Oct 28 '15 at 21:10
  • 1
    @ritesh_NITW, it is even scary when those answers were voted up and one of which was marked as correct :-) – neevek Oct 31 '15 at 07:02
  • This is the correct answer if you're using custom uitablecells from xib files outside of the storyboard – Jim Geldermann Mar 15 '16 at 14:19
  • This **is** a bug. It's not possible to add `UITableViewCell`s to other `UIView`s using IB. – fpg1503 Apr 13 '16 at 22:34
  • @fpg1503, `UITableViewCell` is a subclass of `UIView`, all you need from a `UIView` you can get from a `UITableViewCell`, so why bother using `UIView` while there is a `UITableViewCell` specifically designed for `UITableView`? And why this is **NOT**, I mean **NOT** a bug? because `cellForRowAtIndexPath` was never designed for returning a `UIView`, it returns a `UITableViewCell`. – neevek Apr 14 '16 at 02:09
  • @neevek, The bug is when you're **not** working with `UITableView`s. In my case I'm working with a `UICollectionView` and I have a `UITableViewCell` inside a `UICollectionViewCell` so I can use `UITableView` styles, accessories and so on. So, technically, it's `cellForItemAtIndexPath` returning a `UICollectionViewCell`. The fact that it has a `UITableViewCell` inside shouldn't matter (Liskov substitution principle), unfortunately it does. – fpg1503 Apr 14 '16 at 03:56
  • @neevek, that also happens to `UITableViewCell`s. If you add a `UITableViewCell` as a subview to a `UITableViewCell` and use autolayout this crash happens. – fpg1503 Apr 14 '16 at 03:58
24

I had this issue because I changed the Layout property of the ContentView in the cell to Inferred (Constraints). Switching it back to Autoresizing Mask fixed the issue. enter image description here

in IOS 15, if you have this setting set the wrong way, your app will crash

otusweb
  • 1,638
  • 1
  • 19
  • 30
13

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.

Jose Servet
  • 469
  • 4
  • 7
8

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.

Charles A.
  • 10,685
  • 1
  • 42
  • 39
5

iOS 15 / Xcode 13

This crash happened to me when a .xib file had a UITableViewCell which was NOT nested in a UITableView. The cell was sitting inside a stackview - at one point it was more or less a placeholder so that some interface warnings would go away. I deleted the orphan table view cell & there was no more crash.

Trev14
  • 3,626
  • 2
  • 31
  • 40
  • I've just updated to Xcode 13 / iOS 15 and I'm experiencing the same crash. I have UITableViewCells outside UITableViews, but they are all the root view in their respective .xib files, which according to the older answers should work. From your experience with this problem, did it seem that orphan UITableViewCells are just no longer allowed in any form, even as .xib root? – Arvoreniad Oct 04 '21 at 18:32
  • 1
    My project didn't have any cells at the .xib root, so I wouldn't have an answer for that one. Sorry. – Trev14 Oct 08 '21 at 16:46
4

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.

Note that the top level object in the Xib is a UITableViewCell not a UIView

Daniel Molina
  • 286
  • 3
  • 5
3

Unselecting 'use auto layout' option from the .xib's properties resolved my issue. - Posting what I did as this may help others.

Thank you.

ashNair
  • 105
  • 1
  • 7
3

In my case i was using a UITableViewCell in a UIView to create a custom cell enter image description here

then I deleted the cell from the UIView and then added the all the contents in the UIView directly and that solved my problem.enter image description here

Hope it helps you too. All the best

Kunal Gupta
  • 2,984
  • 31
  • 34
2

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

Harsha
  • 717
  • 1
  • 11
  • 23
2

In iOS15 / Xcode13, you can experience a crash with this error message if you inadvertently assign a custom UITableViewCell class to something that shouldn't be a UITableViewCell.

Custom Class assigned in Interface Builder

In my case, I had accidentally applied TastingCardInstructions – a subclass of UITableViewCell – to both the tastingCardInstructions cell and also to TastingCard, a view within its hierarchy (below).

TastingCard is a UIView, not a cell and therefore it cannot subclass UITableViewCell.

enter image description here

This built and ran fine before upgrading to Xcode13, but the upgrade exposed an old mistake.

mintymuz
  • 262
  • 2
  • 14
0

In my case it was much simpler. Running perfectly with Xcode 12.5 but got the error in 13.1. I had UITableViewCell and an extra view in the contentView. The extra view host a StackView and has as usual the default setting for the constraint 'Inferred (Constraints)'. There must be a change because now the constraint is named 'Inferred (Autoresizing Mask)'. Similar to the nonsense they did to the UIButton. Changing 'Inferred (Constraints)' to 'Autoresizing Mask' solves my problem.

F.Strunk
  • 21
  • 1
  • 3
-1

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.

crubio
  • 6,394
  • 4
  • 30
  • 33