根据所选的 indexpath.row 更改 UICollectionView 单元格图像视图

Posted

技术标签:

【中文标题】根据所选的 indexpath.row 更改 UICollectionView 单元格图像视图【英文标题】:change UICollectionView cell imageview according to the indexpath.row selected 【发布时间】:2016-04-22 22:09:58 【问题描述】:

我有一个显示 5 个单元格的 collectionView。现在我想要完成的是,例如,如果我点击第一个单元格,则下一个单元格的 imageView 是 imageOne,如果我点击第二个单元格,则 imageView 将是 imageTwo。在详细视图控制器中。

我正在努力做到这一点。这是我到目前为止所做的 在 UIView 控制器中的 prepareFor Segue 中,上面有一个 UiCollectionView

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    if([segue.identifier isEqualToString:@"MainPush"])
        NSIndexPath *indexPath = [self.collectionView.indexPathsForSelectedItems objectAtIndex:0];
        NSLog(@"%li", indexPath.row);

// Getting the collectionView cell in the destination class
        if(indexPath.row == 0)
       DetailCollectionViewCell *cell = [[DetailCollectionView alloc] init];
        cell.imageView.image = [UIImage imageNamed: @"Thailand"];
        

    

我点击这个单元格,它会像这样转到另一个 collectionView,但我希望所选 collectionView 中的图像根据所选的 indexpath.row 有所不同。

我这样做了,但什么也没发生

【问题讨论】:

您是否尝试将图像传递给详细视图控制器?换句话说,当一个单元格被选中时,你是否试图转到另一个视图控制器? 【参考方案1】:

假设您正在尝试将 UIimage 传递给详细视图控制器。以下是我的建议。首先,实现这个:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    CurrentImage = [imageArray objectAtIndex:indexPath.row]; //assume what you have an image object and you have an array contains images. 
    [self performSegueWithIdentifier:@"MainPush" sender:nil];

单击单元格时调用此方法。在里面,代码假定你在 mainViewController 中有一个图像对象,并且你有一个包含图像的数组。在它的正下方,调用 performSegue 方法。 记得用你的 ID 在你的故事板中设置 SegueWay

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    if ([[segue identifier] isEqualToString:@"MainPush"]) 
        DetailedController *controller = (DetailedController *)[segue destinationViewController];
        controller.image = CurrentImage;
    

这是一个重要的部分。我假设您的 viewController 的名称是DetailedController。 1. 您必须在目标控制器中创建一个图像对象。 2.当单元格被点击时,它会将currentImage对象设置为用户刚刚点击的图片,并将其传递给DetailViewController。

在DetailController ViewDidLoad 方法中,可以设置UIimageView.image 为image。

【讨论】:

以上是关于根据所选的 indexpath.row 更改 UICollectionView 单元格图像视图的主要内容,如果未能解决你的问题,请参考以下文章

使用 Jquery UI Datepicker 重定向到 URL:根据所选日期更改 URL 值

仅使用 shouldSelectItemAt indexPath 函数选择特定单元格

在 TableView 中查找 [indexpath.row] 值

如果超过 indexpath.row,如何在表格视图中设置单元格行高?

仅根据所选的列表框项加载DataGridView一次

自定义导出Excel表列数据(根据所选的列,选几列导出几列数据,不选则全部导出)