ios - 如何在没有storyBoard的情况下以编程方式将单元格添加到 Collection View
<p><p>我尝试了以下代码:</p>
<pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Pictograma *pictograma;
pictograma = ;
AddPictogramaCell *newCell = [ initWithFrame:CGRectMake(0, 0, 200, 200) pictograma:pictograma];
;
];
newCell.pictograma = pictograma;
return newCell;
}
</code></pre>
<p>但我得到了错误:</p>
<pre><code>Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier - cells must be retrieved by calling -
dequeueReusableCellWithReuseIdentifier:forIndexPath:'
</code></pre>
<p>我不使用 Storyboard,所以我不知道如何解决这个问题。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这里有两个问题。 </p>
<p>首先,当您创建 <code>collectionView</code> 时,您需要添加这一行</p>
<pre><code> forCellWithReuseIdentifier:@"YOUR_IDENTIFIER"];
</code></pre>
<p>然后在您的 <code>collectionView:cellForItemAtIndexPath:</code> 中创建您的单元格,如下所示:</p>
<pre><code>AddPictogramaCell *newCell = (AddPictogramaCell*);
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在没有storyBoard的情况下以编程方式将单元格添加到 Collection View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25377165/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25377165/
</a>
</p>
页:
[1]