简体   繁体   English

单击集合视图上的单元格,如何在详细视图上获得相同的图像?

[英]How to get the same image on detailed view on clicking a cell on collection view?

I am working on an iOS app.I have a collection view which have some images.Images come from an array I have created.When I clicked on a cell of collection view, a second view controller opens.I want to show that particular image on second view controller on which I have clicked on the collection view. 我正在使用iOS应用程序,我有一个包含一些图像的集合视图,图像来自我创建的数组,当我单击集合视图的单元格时,第二个视图控制器打开,我想显示该特定图像在我单击集合视图的第二个视图控制器上。 How to do that? 怎么做?

Create image object in SecondViewController like below: 在SecondViewController中创建图像对象,如下所示:

@property (nonatomic, strong) UIImageView *detailImageView;
@property (nonatomic, strong) UIImage *detailImage;

In your FirstViewController.m do the same as below: 在您的FirstViewController.m中,执行以下操作:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

 CollectionViewCell *cell = [collectionViewObject dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
 SecondViewController *secondViewController = [[SecondViewController alloc] init];
 secondViewController.detailImage = cell.imageView.image;
 [self.navigationController pushViewController:secondViewController animated:YES];
}

And then in SecondViewController.m viewDidLoad method 然后在SecondViewController.m中的viewDidLoad方法

- (void)viewDidLoad
{
 [super viewDidLoad];

  self.detailImageView.image = self.detailImage;
}

Hope this will work for you!! 希望这对您有用!

I hope you have created UIImageView in next ViewController 我希望您在下一个ViewController中创建了UIImageView

Step 1 . 步骤1 Firs of all in you next ViewController.h create image 下一个ViewController.h中所有文件的创建图像

@property (nonatomic, strong) UIImage *image;

then in previous ViewControler.m find you didSelectItemAtIndexPath method, or create them, and add next code: 然后在上一个ViewControler.m中找到您的didSelectItemAtIndexPath方法,或创建它们,然后添加下一个代码:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   MyCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentificer forIndexPath:indexPath];
   MyViewController *myvc = [[MyViewController alloc] init];
   myvc.image = cell.imageView.image;
   [self presentViewController: myvc animated:YES completion:nil];
}

if you don`t using storyboard go to step 3 . 如果您不使用情节提要,请转到步骤3

Step 2. If you using storyboard create image in you next ViewController.h then in previous ViewControler.m find you didSelectItemAtIndexPath method, or create them, and add next code: 第2步。 如果使用情节提要板在下一个ViewController.h中创建图像,则在上一个ViewControler.m中找到didSelectItemAtIndexPath方法,或创建它们,然后添加下一个代码:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   [self performSegueWithIdentifier:@"nextVCSeagueName" sender:self];
}

add method 添加方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"nextVCSeagueName"]) {
        MyviewController *myvc = segue.destinationViewController;
        myvc.image = [imagesArray objectAtIndex:[[self.collectionView indexPathsForSelectedItems] firstObject]];
    }
}

Step 3. In next ViewController.m in viewDidLoad add 步骤3.在下一个ViewController.m中的viewDidLoad添加

-(void)viewDidLoad {
   [super viewDidLoad];

   myImageView.image = self.image;
}

Use the delegate method to know which image to pass- 使用委托方法可以知道要传递的图像-

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

Let's assume your array of images is called imageArray . 假设您的图像数组称为imageArray In your class, declare an image String like- 在您的类中,声明一个图像字符串,例如:

@propert NSString *selectedImage;

Now, when you implement the didSelectItemAtIndexPath method, you can save the image that is loaded in that particular row in your selectedImage variable. 现在,当您实现didSelectItemAtIndexPath方法时,可以将在特定行中加载的图像保存在selectedImage变量中。 Then you can implement the prepareForSegue method to pass this variable to your Second View Controller. 然后,您可以实现prepareForSegue方法,以将该变量传递给您的Second View Controller。

Let's assume, you have declared a variable called imageFromFirstviewController in your SecondViewcontroller.h file. 假设您在SecondViewcontroller.h文件中声明了一个名为imageFromFirstviewController的变量。

@propert NSString *imageFromFirstviewController;

Now, you can do something like following in your didSelectItemAtIndexPath method- 现在,您可以在didSelectItemAtIndexPath方法中执行以下操作-

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedImage = [self.imageArray objectAtIndex:indexPath.row] ;
}

after that pass this image name to the Second View Controller through the segue method. 之后,通过segue方法将此图像名称传递给Second View Controller。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // "my-segue-name" which is assign in the storyboard while attaching the segue
    if ([[segue identifier] isEqualToString:@"my-segue-name"])
    {   
        SecondViewcontroller *secondVC = [segue destinationViewController];
        secondVC.imageFromFirstviewController = self.selectedImage;
    }
} 

Now, as you have access to your selected image in your second view controller, you can use this imageFromFirstviewController anyway you want. 现在,由于您可以访问第二个视图控制器中的选定图像,因此可以随时使用此imageFromFirstviewController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在集合视图单元格中更改图像视图 - How to change Image view in collection view cell 如何在集合视图单元格中更改图像 - How to change image in a collection view cell 在swift 3中单击另一个表视图单元格的集合视图数据后,如何在表视图单元格中重新加载集合视图数据? - How to reload the collection view data in table view cell after clicking another table view cell's collection view data in swift 3? 如何通过单击表格视图单元格进入另一个视图 - How to get onto another view from clicking on a table view cell 如何在sizeForItem中获得自定义集合视图单元格 - How to get a custom collection view cell in sizeForItem 如何获取集合视图单元格的indexPath - How to get the indexPath of a collection view cell 图像未加载到集合视图单元格中 - Image not loading in collection view cell 集合视图内的两个方向单元格,如何获取单元格标题? - Two directional cell inside collection view , how to get cell title? 单击表格视图中的单元格时如何在地图视图上获取注释? - How to get annotations on a map view while clicking a cell in a tableview? 如何将图像从ios中的本地库添加到集合视图单元格中? - How to add image into collection view cell from local library in ios?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM