title: Setting .reuseIdentifier on a UICollectionViewCell
body: <p>I have a particular <code>UICollectionViewCell</code> that I want to <strong>instantiate myself</strong>, and add to a <code>UICollectionView</code>. In order for this to work, the <code>UICollectionViewCell</code> instance needs its <code>.reuseIdentifier</code> property to be set.</p>
  
  <p>Normally, the class or Nib that describes the cell is registered with the collection view, and the collection view instantiates the cell with its <code>.reuseIdentifier</code> already set, with these methods:</p>
  
  <pre><code>- registerClass:forCellWithReuseIdentifier:
  - registerNib:forCellWithReuseIdentifier:
  </code></pre>
  
  <p>However, since I am constructing this cell outside of the collection view, these do not apply.</p>
  
  <p>When I create the cell myself, there appears to be no way to set its <code>.reuseIdentifier</code> (because it is a readonly property, and there are no <code>init...</code> methods that initialize it).</p>
  
  <p>When <code>.reuseIdentifier</code> is not set, the <code>UICollectionView</code> throws an exception when the cell is added. This behavior is different from <code>UITableView</code>, where reuse identifiers were optional.</p>
  
  <p>An easy workaround to set the collection view cell's reuse identifier is to embed it in a <code>.xib</code> file and use the <code>Identifier</code> box, then create an instance of the cell with</p>
  
  <pre><code>[NSBundle.mainBundle loadNibNamed:@"MyCellName" owner:self options:nil][0];
  </code></pre>
  
  <p>I can then pass the above-instantiated <code>UICollectionViewCell</code> and everything works fine.</p>
  
  <p>...but that seems like a pretty silly and arbitrary hoop to jump through. Is there some other way to get this property set on the cell instance without the <code>.xib</code>-wrapper detour?</p>
  
  <hr>
  
  <p>Update: Apple's documentation says this:</p>
  
  <blockquote>
    <p>To simplify the creation process for your code, the collection view requires that you always dequeue views, rather than create them explicitly in your code.</p>
  </blockquote>
  
  <p>...which is actually not true, because it doesn't require this (i.e., externally-instanced cells work fine as long as their identifier is set <em>somehow</em>, e.g. if loaded from a <code>.xib</code>), and it also doesn't "simplify the creation process for my code" in my particular use case (rather requires an extra file; further it would be messy to require that the collection view create these few complex one-offs).</p>
  
  <p>But the above does seem to imply that the answer to the question is no: it's intentionally "difficult" to create a usable cell except by having the collection view dequeue it.</p>