簡體   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