如何将我的id单击CollectionViewCell传递给其他ViewController

时间:2016-09-06 06:11:55

标签: ios objective-c iphone uicollectionview

我在我的应用中创建了图库视图。在该添加段控制器中,第一个索引是表视图,第二个索引是集合视图。第一个索引(表视图)在我的应用程序中正常工作,但第二个索引(集合视图)显示数组存储的图片,但我想点击集合视图单元格并移至另一个视图并将我的UID传递给另一个视图控制器。我尝试但无法通过我的身份证明。请帮忙。谢谢

Segue方法

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

  if ([segue.identifier isEqualToString:@"Tableview"]) {
      NSIndexPath *indexPath = [self.table_view indexPathForSelectedRow];
      Detail_view *destViewController = segue.destinationViewController;
      destViewController.uid_value = [id_ary objectAtIndex:indexPath.row];
  }
  else if ([segue.identifier isEqualToString:@"Collectionview"])
  {
    NSArray *indexPaths = [self.collection_view indexPathsForSelectedItems];
    collection *destViewController = segue.destinationViewController;
    NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
    destViewController.uid_value = [id_ary[indexPath.section] objectAtIndex:indexPath.row];
    [self.collection_view deselectItemAtIndexPath:indexPath animated:NO];
  }
}

2 个答案:

答案 0 :(得分:0)

我认为您需要改进代码,而不是更好地对用户进行拆分数组NSObject类使用Object Class管理数组,以便在代码下使用解决方案

else if ([segue.identifier isEqualToString:@"Collectionview"])
{
   NSArray *indexPaths = [self.collection_view indexPathsForSelectedItems];
   collection *destViewController = segue.destinationViewController;
   NSIndexPath *indexPath = [indexPaths firstObject]; // you can also use firstObject rather then ObjectAtIndex:0
   destViewController.uid_value = [id_ary objectAtIndex:indexPath.row];
   [self.collection_view deselectItemAtIndexPath:indexPath animated:NO];
}

答案 1 :(得分:0)

试试这个 -

为ex提取全局indexPath NSIndexPath *selectedIndexPath;现在在didSelectItemAtIndexPath添加此行,即

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

现在使用prepareForSegue方法 -

else if ([segue.identifier isEqualToString:@"Collectionview"])
{
   collection *destViewController = segue.destinationViewController;
   destViewController.uid_value = [id_ary objectAtIndex:selectedIndexPath.row]; 
   // or destViewController.uid_value = [id_ary[selectedIndexPath.section] objectAtIndex:selectedIndexPath.row];
   [self.collection_view deselectItemAtIndexPath:selectedIndexPath animated:NO];    
}

希望这能解决你的问题.. :)