1

拥有集合视图并尝试在集合视图单元格中显示组文件夹中的图像。索引方面,它应该从 0 开始,但它显示来自索引 1 的图像,并且索引 0 处的集合视图单元格显示为空。

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
return 6;
 }

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


 Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];

// make the cell's title the actual NSIndexPath value
cell.label.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section];

// load the image for this cell
NSString *imageToLoad = [NSString stringWithFormat:@"%d.jpg", indexPath.row];
cell.image.image = [UIImage imageNamed:imageToLoad];

return cell;
}

  // the user tapped a collection item, load and set the image on the detail view controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
if ([[segue identifier] isEqualToString:@"showDetail"])
{
    NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];

    // load the image, to prevent it from being cached we use 'initWithContentsOfFile'
    NSString *imageNameToLoad = [NSString stringWithFormat:@"%d_full", selectedIndexPath.row];
    NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"jpg"];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];

    DetailViewController *detailViewController = [segue destinationViewController];
    detailViewController.image = image;
}
 }

为什么索引 0 处的单元格没有显示图像。集合视图显示来自索引 1 的单元格的图像,但从 0 开始除外。我在我的代码中做错了什么。

感谢所有帮助。

4

0 回答 0