简体   繁体   中英

tableView in collectionView IOS

I'm using Collection View in a MainStoryboard.storyboard showing a grid of images. I want to go from collection view to table view when the item is selected, ie on following method:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

How can I load tableView in collectionView's method after selecting?

My approach:

UIViewController *vc = [[UIStoryboard storyboardWithName:@"SubCategories" bundle:nil] instantiateViewControllerWithIdentifier:@"SubCategories"];
 [self.view  addSubview:vc.view ];

You should make a new ViewController in your Storyboard that has a TableView in it. Then add a segue from the first ViewController (from the VC itself, not from the CollectionView) to the new ViewController. Make sure you set a segue identifier for that segue.

Then in your didSelectItemAtIndexPath method, call

[self performSegueWithIdentifier:@"YourIdentifier" sender:self];

You can then implement the prepareForSegue method in your first VC to pass data about what was selected to your second VC.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Make sure your are about to perform the same segue
    if ([[segue identifier] isEqualToString:@"YourIdentifier"]) {
        // Get reference to the destination view controller
        SecondViewController *vc = [segue destinationViewController];
        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

Note, don't ever call prepareForSegue, iOS calls it for you when a segue is about to happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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