13

I managed to figure out the approach for self-sizing collection view cells under iOS 8.

I want to do this as a part of a accessory view.

I get a crash ... the interesting part of the stacktrace is as follows:

** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
  0   CoreFoundation    __exceptionPreprocess + 165
  1   libobjc.A.dylib   objc_exception_throw + 45
  2   CoreFoundation    -[__NSArrayM insertObject:atIndex:] + 954
  3   UIKit             -[UICollectionViewFlowLayout layoutAttributesForElementsInRect:] + 384
  4   UIKit             __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 144
  5   UIKit             -[UICollectionViewData validateLayoutInRect:] + 1396
  6   UIKit             -[UICollectionView layoutSubviews] + 170
  7   UIKit             -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
  8   QuartzCore        -[CALayer layoutSublayers] + 150
  9   QuartzCore        _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
  10  UIKit             -[UIView(Hierarchy) layoutBelowIfNeeded] + 611
  11  UIKit             -[UIInputSetHostView layoutIfNeeded] + 105
  12  UIKit             __43-[UIInputWindowController setInputViewSet:]_block_invoke + 112
  13  UIKit             +[UIView(Animation) performWithoutAnimation:] + 65
  14  UIKit             -[UIInputWindowController setInputViewSet:] + 291
  15  UIKit             -[UIInputWindowController performOperations:withAnimationStyle:] + 50
  16  UIKit             -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1054
  17  UIKit             -[UIResponder becomeFirstResponder] + 468
  18  UIKit             -[UIView(Hierarchy) becomeFirstResponder] + 99
  19  UIKit             -[UITextView becomeFirstResponder] + 75
  ...

This looks like an Apple bug. Why does the layout seem to return an empty layout attribute?

fatuhoku
  • 4,815
  • 3
  • 30
  • 70
  • I had the same issue a few days ago , you could try to delete the derived data then rebuild. – Ezimet Aug 23 '14 at 11:15
  • Unfortunately that's not helped. – fatuhoku Aug 25 '14 at 09:25
  • I have isolated the problem to the way that I'm using the header... still. So baffling. – fatuhoku Aug 25 '14 at 09:29
  • 1
    If I set up a header with some fixed item size, it works fine. If I use estimatedItemSize instead, then this error happens. My conclusion is that `estimatedItemSize` works but just not with `headerReferenceSize` – fatuhoku Aug 25 '14 at 11:11
  • 3
    Have you filed a radar for this? – Mike Welsh Sep 30 '14 at 20:34
  • 1
    Did anyone ever manage to find a workaround for this? I'm unable to use headers with self sizing layouts as things stand. @fatuhoko, when you say "If I set up a header with some fixed item size, it works fine" - how are you doing that? – Adam Waite Oct 27 '14 at 10:46
  • 1
    What I meant was, if you forget about using `estimatedItemSize` to self-size cells altogether, and just size cells by setting the `itemSize` property instead, then you headers work. i.e. the old functionality of using either the property, or overriding `itemSizeForIndexPath` or whatever it is, still works. – fatuhoku Oct 29 '14 at 11:15
  • just had the same experience. As you indicated, it works for me if the section header is not being used. Did you set up a radar, so that we at least can hope? – Red Dec 04 '14 at 18:49
  • Same behaviour for me. – Robert Atkins Dec 08 '14 at 10:36
  • 1
    Have anyone found a workaround for this? – mato Dec 09 '14 at 02:44
  • Is there some other way to hack a header or footer into a uicollectionview with self-sizing cells? – webmagnets Feb 24 '15 at 22:19
  • For what it's worth. This appears to have been fixed in iOS 8.3. However, is still a problem in iOS < 8.3. – Mark T. May 20 '15 at 04:01
  • Hmm, I'm still getting this issue in iOS 8.4, but not in iOS 9. – brynbodayle Dec 15 '15 at 17:39

1 Answers1

1

The best idea I have so far is based on this repository: https://github.com/algal/SelfSizingCellsDemo

On line 50 of ViewController.swift we have label.preferredMaxLayoutWidth = 320 which produces a cell that fills the whole screen and wraps the lines if there is enough text. This would need to be changed to fit whatever size screen you are working with. Then after each section you would need to add enough text to fill that label like is done on line 20 let items = smallitems.componentsSeparatedByString(" ") + [onelongitem]

The problem with this approach is that I don't yet know how I would put different views in that cell, besides text. This might work enough for your situation though.

webmagnets
  • 2,266
  • 3
  • 33
  • 60