OGeek|极客世界-中国程序员成长平台

标题: ios - 不为 collectionView 调用 cellForItemAtIndexPath [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:23
标题: ios - 不为 collectionView 调用 cellForItemAtIndexPath

我在 Storyboard 中采用了 UICollectionView。我还设置了 datasourcedelegate。 下面的方法被调用

 - (NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section{
 return 4;
}

但是 - (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath 没有被调用。

附上 UICollectionView 的属性截图。关于这个问题的任何想法? image 1

enter image description here

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

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

将尝试通过打破约束来恢复

在 UIViewAlertForUnsatisfiableConstraints 处创建一个符号断点,以在调试器中捕获它。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。



Best Answer-推荐答案


我认为你没有正确设置流出。

因此,它不会转到 - (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath 方法。所以,设置断点并检查每一个流程布局方法。

流式布局方法的顺序调用,

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

 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    float ratio = self.aCollectionView.frame.size.width/3;
    [flowLayout setItemSize:CGSizeMake(ratio, ratio)];
    [flowLayout setMinimumInteritemSpacing:0];
    [flowLayout setMinimumLineSpacing:0]; 

第二,在您的情况下,此方法也称为

- (NSInteger)collectionViewUICollectionView *)view numberOfItemsInSectionNSInteger)section

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

- (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);
}

4、这个方法调用

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

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

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath

关于ios - 不为 collectionView 调用 cellForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161779/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4