IOS: Storyboard CollectionView not called

I have a UICollectionView controller built into the navigation controller. CollectionView displays projects, and each cell should go to the ProjectDetail screen.

I just can't get the shogi to call. If I just drop the button on the navigation bar and connect to the details, it will work. But there is no CollectionView running from my cell.

Here's what the storyboard looks like: http://cl.ly/RfcM I have a segue connected from CollectionViewCell to ProjectDetailViewController

Here is the corresponding code inside my ProjectDetailViewController:

@interface ProjectCollectionViewController () { NSArray *feedPhotos; Projects *projects; } @end @implementation ProjectCollectionViewController - (void)viewDidLoad { [super viewDidLoad]; [self.collectionView registerClass:[FeedViewCell class] forCellWithReuseIdentifier:@"cell"]; [self loadData]; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"selected %d", indexPath.row); Project *project = [projects getProject:indexPath.row]; NSLog(@"project = %@", project); } - (void)loadData { [self.projectLoader loadFeed:self.username onSuccess:^(Projects *loadedProjects) { NSLog(@"view did load on success : projects %@", loadedProjects); projects = loadedProjects; [self.collectionView reloadData]; } onFailure:^(NSError *error) { [self handleConnectionError:error]; }]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return projects.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"cell"; FeedViewCell *cell = (FeedViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0]; UIImageView *cellImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; Project *project = [projects getProject:indexPath.row]; NSString *imageUrl = [project coverPhotoUrl:200 forHeight:200]; NSLog(@"imageurl =>%@", imageUrl); if (imageUrl) { [cellImageView setImageWithURL:[NSURL URLWithString:imageUrl]]; } [cell addSubview:cellImageView]; cell.imageView = cellImageView; return cell; } 

I assume the problem is how I connect cells to CollectionView.

Any help would be greatly appreciated!

+8
ios uistoryboard uicollectionview
source share
4 answers

You cannot create segues directly from cells in the storyboard because the collection is collected dynamically through the data source. You should use collectionView:didSelectItemAtIndexPath: and programmatically execute segue using performSegueWithIdentifier:sender: Something like that:

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

where MySegueIdentifier is the identifier of the segment defined in the storyboard.

+21
source share

TL; DR: FOR THE PARTY, do not call registerClass: forCellWithReuseIdentifier :. It overrides what the storyboard for the cell creates (including how the segues are processed): How to set a UILabel in a UICollectionViewCell

Brief setup

  • Used a storyboard
  • Created a new collection view controller using the Xcode template, setting it as a subclass of UICollectionViewController.
  • Initially, the default UICollectionViewCell was used, adding a UILabel programmatically.

The generated UICollectionViewController code registered the cell in viewDidLoad:

 [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 

First problem: Preparation process ForSegue: sender: the event did not fire, and this led me to such an answer. I implemented UICollectionViewDelegate and collectionView: didSelectItemAtIndexPath: event, and then segue is called programmatically. This fixed my first problem.

Second problem: I switched to a custom cell containing one label. After everything was connected, the cell label was not displayed. After some digging, I found the solution contained in the link at the top of my answer.

Third release and solution: I deleted registerClass: forCellWithReuseIdentifier: line. When I started the application, the shortcut appeared correctly, but when I clicked on the cell, it raised the prepareForSegue: sender event twice. By removing the registerClass: forCellWithReuseIdentifier line, the cell directly processed the cells, without the need for a delegate method. This is how I expected the storyboard to work. I removed collectionView: didSelectItemAtIndexPath: an event that allowed double-shot prepareForSegue: sender :. If you are using a storyboard, do not register the cell class. It overwrites the structure of the storyboard.

+3
source share

Did you create a CellView Cell connection in Triggered Segues on selection ?

You can also program segue with [self performSegueWithIdentifier:@"segueIdentifier" sender:nil]

in

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

+2
source share

Equivalent Swift code for a similar question.

 override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { self.performSegueWithIdentifier(@"TargetSegway", sender: self) } 

Make sure that if your cell has other overlapping views, โ€œUser Interaction Enabledโ€ is not checked (you can find this option in the View / Interaction attribute inspector window). Otherwise, your token gesture is consumed by an overlapping view, didSelectItemAtIndexPath cannot be called.

0
source share

All Articles