Search code examples
iosuitableviewautolayoutnsfetchedresultscontrolleruitableviewautomaticdimension

UITableview with Autolayout and NSFetchedResults controller cell layout bug on insert


I seem to be having a very strange problem with my UITableview cells and autolayout. So my tableview is set up as follows:

  • I have a UITableView within a UIViewController that I created via Interface builder using Autolayout
  • My tableview is using UITableViewAutomaticDimension so that the cells can resize based on the text within them.
  • Each cell has 2 subviews within it. A front view and a back view. The back view contains the buttons for my slide-able cells
  • I am using NSFetchedResults controller to update the tableview

The problem occurs when I tap on a cell and am taken to the next view. Then I create a core data item within that view and save so that it is inserted into my tableview (now behind the current view controller).

What happens is that the newly inserted cell seems to lose all autolayout constraints and all view elements are on top of each other in the top left of the cell. It also looks like they are not even contained within the front view for the cell (or the front view has a width and height of 0).

Example:

Example

It gets even stranger. If I refresh the tableview by pulling down and the cells are reloaded it corrects itself. However, if I refresh the tableview again then the problem appears again but on a different cell. This bug does not happen consistantly which is rather frustrating.

Any help would be appreciated as I only have a few remaining hairs on my head to pull out at this stage.

Thanks in advance!


Solution

  • So I found out what the issue was. The issue was that I was using size classes in Interface builder but I was only using the iPhone Portrait size class and did all my work in there instead of the "any" size class. Disabling size classes and setting the project to iPhone only fixed all these problems.

    Weird I know