菜鸟教程小白 发表于 2022-12-12 11:23:05

ios - 不为 collectionView 调用 cellForItemAtIndexPath


                                            <p><p>我在 Storyboard 中采用了 <code>UICollectionView</code>。我还设置了 <code>datasource</code> 和 <code>delegate</code>。
下面的方法被调用</p>

<pre><code> - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 4;
}
</code></pre>

<p>但是 <code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath</code> 没有被调用。 </p>

<p>附上 <code>UICollectionView</code> 的属性截图。关于这个问题的任何想法? <a href="/image/XpDoW.png" rel="noreferrer noopener nofollow"><img src="/image/XpDoW.png" alt="image 1"/></a> </p>

<p> <a href="/image/e0S3t.png" rel="noreferrer noopener nofollow"><img src="/image/e0S3t.png" alt="enter image description here"/></a> </p>

<p>如果我隐藏导航栏,现在 collectionView 是可见的。在设置顶部约束 <code>Myheader Collection View.top = Top Layout Guide.bottom + 64</code> 认为它位于 Controller 顶部。并且我在完成方法 -<code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath</code> 后收到以下警告。</p>

<p><strong>警告:</strong>
能够同时满足约束。
    可能至少以下列表中的约束之一是您不想要的。
    尝试这个:
      (1)查看每个约束并尝试找出您不期望的;
      (2) 找到添加了不需要的约束或约束的代码并修复它。
(
    “<_UILayoutSupportConstraint:0x7fda80dcff20 V:>”,
    “<_UILayoutSupportConstraint:0x7fda80daca10 V:|-(0)-(名称:'|':UIView:0x7fda80efb580)>”,
    "",
    “”
)</p>

<p>将尝试通过打破约束来恢复
</p>

<p>在 UIViewAlertForUnsatisfiableConstraints 处创建一个符号断点,以在调试器中捕获它。
中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为你没有正确设置流出。</p>

<p>因此,它不会转到 <code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath</code> 方法。所以,设置断点并检查每一个流程布局方法。</p>

<p>流式布局方法的顺序调用,</p>

<p><code>viewdidload</code> 中的第一个 customflowlayout 将被调用,//如果您已设置</p>

<pre><code> UICollectionViewFlowLayout *flowLayout = [ init];
    ;
    float ratio = self.aCollectionView.frame.size.width/3;
    ;
    ;
    ;
</code></pre>

<p>第二,在您的情况下,此方法也称为 </p>

<p><code>- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section</code></p>

<p>第三 - 你需要从这一行检查,//你是否设置正确</p>

<pre><code>- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    float width = collectionView.frame.size.width/3;//120
    float height = collectionView.frame.size.height/4;//120
    return CGSizeMake(width-17,height-14);
}
</code></pre>

<p>4、这个方法调用</p>

<pre><code>- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(14.0, 14.0, 0.0, 14.0);
}
</code></pre>

<p>最后这里调用了<code>cellforitem</code>方法。所以,你的这个方法没有被调用,问题是在上面的方法中尝试设置断点并改变单元格大小和边缘昆虫等然后尝试,</p>

<p>5、如果以上设置正确,则调用下面的方法。</p>

<p><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath</code></p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 不为 collectionView 调用 cellForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37161779/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37161779/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 不为 collectionView 调用 cellForItemAtIndexPath