我在导航控制器中嵌入了一个UICollectionView控制器. collectionView列出项目,每个单元格应该转到ProjectDetail屏幕.
我根本无法触发segue.如果我只是在导航栏上放下一个按钮并将细节与细节挂钩,它就能正常工作.但是从我的CollectionView单元格触发却没有.
这是故事板的样子:http://cl.ly/RfcM我有一个从CollectionViewCell连接到ProjectDetailViewController的segue
这是我的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,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; }
我猜这个问题在于我如何将Cell连接到CollectionView.
任何帮助将不胜感激!
解决方法
您无法直接从故事板中的单元格创建segue,因为集合视图是通过数据源动态填充的.您应该使用
collectionView:didSelectItemAtIndexPath:
并使用
performSegueWithIdentifier:sender:
以编程方式执行segue.这样的事情:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self]; }
其中MySegueIdentifier是storyboard中定义的segue的标识符.