iOS: indexPath.row, UICollectionView, and Images



我试图通过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;
    }

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

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

最新更新