简体   繁体   English

如何将值从嵌入式collectionView中的选定单元格传递到另一个viewController?

[英]How to pass value from a selected cell in embedded collectionView to another viewController?

In UIViewController 1 , I have set up an array. UIViewController 1 ,我设置了一个数组。 This UIViewController segues to UIViewController 2 . UIViewControllerUIViewController 2

In UIViewController 2 , there is a UITableView with custom UITableViewCell . UIViewController 2 ,有UITableView自定义UITableViewCell There's also a UIButton which segues perfectly fine back to UIViewController 1 . 还有一个UIButton可以完美地返回UIViewController 1

In the custom cell, there is a collectionView . 在自定义单元格中,有一个collectionView This is populated by the array from ViewController 1 . 这是由ViewController 1的数组填充的。

My question is, when an item is selected in the collectionView ( UIViewController 2 - custom UITableViewCell class), how to pass that value all the way back to UIViewController 1 ? 我的问题是,当在collectionViewUIViewController 2自定义UITableViewCell类)中选择了一个项目时,如何将该值一直传递回UIViewController 1

I'm sorry if this is repetitive. 如果这是重复的,我很抱歉。 I've referred to many similar entries here but nothing seems to be working. 我在这里提到了许多类似的条目,但似乎没有任何效果。 I've also tried this: 我也尝试过这个:

http://www.appcoda.com/ios-collection-view-tutorial/ http://www.appcoda.com/ios-collection-view-tutorial/

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"showRecipePhoto"]) {
    NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
    RecipeViewController *destViewController = segue.destinationViewController;
    NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
    destViewController.recipeImageName = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
  }
}

I keep getting the null value returned and I'm not sure why. 我一直在返回空值,但我不确定为什么。

(I'm not using storyboard. And this is my first attempt at programming of any kind. Would appreciate any input!) (我不使用情节提要。这是我第一次尝试任何类型的编程。不胜感激!)

You can do it either with delegation or completion block. 您可以使用委派或完成块来完成。

To solve with delegation please follow this link . 要与代表团一起解决,请点击此链接

To solve with completion block please follow this link . 要解决完工问题,请点击此链接

You can use UICollectionViewDelegate . 您可以使用UICollectionViewDelegate

Add in your class: 添加您的课程:

class YourViewController: UIViewController, UICollectionViewDelegate { ... }

and use - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath this event is called when the cell is selected; 并使用- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath选择单元格时将调用此事件; you must save the value in a property; 您必须将值保存在属性中; like that: 像那样:

- (void)collectionView:(UICollectionView *)collectionView 
didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{
   selectedRecipeImageName = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
... 
   [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

and then: 接着:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"showRecipePhoto"]) {
    RecipeViewController *destViewController = segue.destinationViewController;
    destViewController.recipeImageName = selectedRecipeImageName
  }
}

暂无
暂无

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

相关问题 如何将选定单元格的值传递给另一个ViewController? - How pass the value of a selected cell to another ViewController? 将数据从CollectionView委托传递到另一个ViewController - Pass data from CollectionView Delegate to another ViewController 如何将数据从单元格传递到另一个 ViewController - how to pass data from cell to another ViewController 将Json响应从一个Viewcontroller传递到另一个Viewcontroller并填充CollectionView - Pass Json response from one Viewcontroller to another Viewcontroller and populate CollectionView 如何使用委托从 Tableview 单元格内的 Collectionview 单元格导航到另一个 ViewController? - How to Navigate to another ViewController from Collectionview cell which is inside Tableview cell using delegate? 通过segue将CollectionView的选定单元格索引发送到viewController - Send selected cell index of CollectionView to viewController by segue 如何将值传递给嵌入在 NavigationController 中的 viewController - How to pass value to a viewController embedded in NavigationController 如何在Swift中将值从自定义单元格设置/传递给ViewController - How to set/pass value from a custom cell to a viewcontroller in Swift 如何将选定的行数据传递给另一个ViewController - How to pass selected row data to another ViewController [以编程方式]选择嵌入在 collectionView 单元格中的 tableView 行时推送 viewController - [Programmatically]Push a viewController when selecting tableView row embedded in a collectionView cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM