【问题标题】:iOS: indexPath.row, UICollectionView, and ImagesiOS:indexPath.row、UICollectionView 和图像
【发布时间】:2013-04-06 01:00:12
【问题描述】:

我试图通过UICollectionView 在我的主 iphone 屏幕上仅显示 9 张图片。但是我的显示有问题。我的第一张图片总是重复 2 次(在第 1 行的位置 1 和位置 2)。

我的代码有问题吗?

// Define the number of Cell.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return  9; }


// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *IdCell = @"Cell";

     SSCollectionViewCell *cell = (SSCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier: IdCell forIndexPath: indexPath];
    int imageNumber = indexPath.row % 9;

    cell.imageView.image = [UIImage imageNamed: [NSString stringWithFormat:@"M2_img_%d@2x.png", imageNumber]];

    return cell;

    }

【问题讨论】:

  • 代码看起来不错...检查你的图片 M2_img_0@2x.png & M2_img_1@2x.png
  • [评论因不准确而被删除]
  • 我的图片没问题,而且命名正确... :(
  • 解决方案:清理并清除所有项目。

标签: iphone ios uiviewcontroller


【解决方案1】:

您不需要指定 UICollectionView 中有多少个部分吗?您已经有了委托方法 numberOfItemsInSection,对吧?现在,在 numberOfSectionsInCollectionView 委托方法中,您指定了 1 吗?如:

  • (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { 返回 1; //如果它只有 9 个图像 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2018-12-09
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多