3

I am in a loop with myself on from where to get the cell's size in a collection view in case I have a cell with auto layout

I understand that sizeForItemAtIndexPath should get the size from the data and not from the cell itself, because the cell is not drawn yet, but in auto layout I can't calculate this just by looking at the data, unless I put in code the contraints in a way that I can later calculate from them (seems crooked)

On the other hand, UIView does have intrinsicContentSize and systemLayoutSizeFittingSize which gives the size of a view that's already drawn or going to be drawn.

But in sizeForItemAtIndexPath I don't have a view yet, and the data is just data.

the cell nib looks like this enter image description here

In what way should I get the size of a custom cell (from .nib)?

I could technically hard code sizes for the images and icons, but that feels wrong. It also feels wrong to ask the view what size it is in a function that should tell the controller what size it is

Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244
  • What data are you trying to display? There are a **lot** of ways to get the size that data **will** take up on screen. For text, you can get the bounding rect for an `NSAttributedString`. For an image, you can figure out how big you **want** it to be on screen, etc. You can also instantiate a proxy view that you can reuse for sizing purposes (in my testing, you can't instantiate a cell for this purpose). Give me some more details and I'll try to get you a more concrete answer. – mbm29414 Feb 11 '15 at 13:54
  • I added a screenshot of the nib – Nick Ginanto Feb 11 '15 at 14:03
  • Ah, ok. If you have a custom XIB, see this answer: http://stackoverflow.com/a/28161915/394484. Should give you what you want. – mbm29414 Feb 11 '15 at 14:12

1 Answers1

2

You can use an off screen prototype cell which you configure and then use to determine its size.

This is a common approach for UITableView pre iOS8 to determine cell height. The same approach should work for UICollectionView.

See the accepted answer here: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • When I tried a similar method for `UICollectionViewCell`, it proved impossible. See here: http://stackoverflow.com/a/28161915/394484 – mbm29414 Feb 11 '15 at 14:11
  • Perhaps the issue for `UICollectionViewCell` layout is that the width is not set when doing it manually. With UITableViewCell the width is known. Perhaps fixing the width would allow correct height sizing? Just a thought. However what width to use. – Rory McKinnel Feb 11 '15 at 14:44
  • 4
    Actually, there's a problem with dequeueing a collection view cell except in `cellForItemAtIndexPath:`. For some reason, this crashes. I agree it's unexpected, but that's what we discovered in the answer I linked. – mbm29414 Feb 11 '15 at 14:46
  • Ok. With UITableView you use the non index path version of the dequeue cell function which has no dependency on index path. Its possible that using the index path is what caused the crash. Shame UICollectionView has no non index path version. Perhaps internally it has issues dequeuing for a cell which it has not asked for yet. Loading your own cell prototype via storyboard/nib though should achieve the same thing as a non index path dequeue, which seems to be what you did. Again problem is what width I think as that ultimately decides the height required with wrapping text etc... – Rory McKinnel Feb 11 '15 at 15:04
  • For sure. But I think most people are basing the width of a cell off of the device itself, which will work. Given how `UICollectionView` does wrapping, that's how I've always approached the width issue. – mbm29414 Feb 11 '15 at 18:49