繁体   English   中英

如何将图像从集合视图传递到iphone中的详细视图

[英]how to pass an image from collection view to detail view in iphone

从Remote XML中检索数据,我可以在集合视图中显示图像。 从这个集合视图我试图显示一个详细的视图控制器,如果在集合视图控制器中单击一个图像,它应该在详细视图中显示图像。 我被卡在这里,我无法显示详细视图控制器。 我哪里错了? 请指导我。

下面是我的Collection视图Controller,

- (void)viewDidLoad
  {
   [super viewDidLoad];
   xmlParser = [[MyXMLParser alloc] loadXMLByURL:@"http://MyXml.com/sample.xml"];
   myarray = xmlParser.arrayofimages;
  }

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  {
    return 1;
  }

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  {
    return [myarray count];
  }

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
  {
     MyCollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
     if (!myCell) {
         myCell = [[MyCollectionViewCell alloc] initWithFrame:CGRectMake(0,0,myCell.frame.size.width , myCell.frame.size.height)];
     }
    [myCell.tweetImageView setImage:[myarray objectAtIndex:indexPath.row]];
    return myCell;
  }

- (void)collectionView:(UICollectionViewCell *)collectionView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
    DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"MyCell" bundle:[NSBundle mainBundle]];
    detailVC.img= [myarray objectAtIndex: indexPath.row];
    [self presentViewController:detailVC animated:YES completion:nil];
  }

DetailView控制器

-(void)viewDidLoad
  {
   [super viewDidLoad];
   self.imageView.image = self.img;
  }

试试Collection View Controller,

[self.navigationController pushViewController:detailVC animated:YES];

你错过了这一行。

您没有展示您的detailview控制器。 你只是分配它。

代替:

- (void)collectionView:(UICollectionViewCell *)collectionView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
    DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"MyCell" bundle:[NSBundle mainBundle]];
    detailVC.img= [myarray objectAtIndex: indexPath.row];
  }

校验:

- (void)collectionView:(UICollectionViewCell *)collectionView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
    DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"MyCell" bundle:[NSBundle mainBundle]];
    detailVC.img= [myarray objectAtIndex: indexPath.row];
   [self presentViewController:detailVC animated:YES completion:nil];
  }

尝试这个。 在显示视图后分配图像

DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"MyCell" bundle:[NSBundle mainBundle]];
       [self presentViewController:detailVC animated:YES completion:nil];
    detailVC.img= [myarray objectAtIndex: indexPath.row];

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM