Question

My setup looks like this:

I have a UIViewController with differnt elements on top of it including one UIContainerView. This container has a UICollectionView with 9 UICollectionViewCell embed in it. All of these cells have one UILabel on it. The cells are all selectable. So what I basically did is created a multi selection element. My question is, how to pass the selected cells (best as NSArray with the strings of all selected elements) to the parent, the UIViewController?

What I did so far is create an IBOutlet to the container in the top UIViewController:

@property (weak, nonatomic) IBOutlet MultiSelectionContainerView *multiSelectionCV;

I'd like to be able to acces the array of selected cells like this:

NSArray* selectedCells = [_multiSelectionCV getSelection];

How do I best do that?

Was it helpful?

Solution

I assume your MultiSelectionContainerView has a UICollectionView ember in it , In your MultiSelectionContainerView:

 - (NSArray *)getSelection{
    NSMutableArray *selectedItems = [[NSMutableArray alloc] init];
    for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]){
        YourCollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
        [selectedItems addObject:cell.label.text];
    }
    return selectedItems;
}

EDIT:

You have to pass these layers level by level, like UIViewController holds MultiSelectionContainerView as property,MultiSelectionContainerView holds MultiSelectionViewController,and you implement my answer in MultiSelectionViewController.then you can access it via

 [self. multiSelectionCV.multiSelectionVC getSelection]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top