iOS:Storyboard CollectionView不会被触发

我有一个导航控制器内embedded的UICollectionView控制器。 collectionView列出项目,每个单元格应该延伸到ProjectDetail屏幕。

我根本无法得到继续触发。 如果我简单地在导航栏上放一个button,然后连接细节,它就可以工作。 但从我的CollectionView单元格触发不。

这里是故事板的样子: http : //cl.ly/RfcM我有一个从CollectionViewCell连接到ProjectDetailViewController

以下是我的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; } 

我猜这个问题是在我如何把单元格连接到CollectionView的地方。

任何帮助将不胜感激!

您不能直接从故事板中的单元格创buildsegse,因为collectionview是通过数据源dynamic填充的。 您应该使用collectionView:didSelectItemAtIndexPath:并使用performSegueWithIdentifier:sender:编程执行segue。 像这样的东西:

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

MySegueIdentifier是在storyboard中定义的segue的标识符。

TLDR:对于一个故事板,不要​​调用registerClass:forCellWithReuseIdentifier :. 它覆盖故事板为单元格设置的内容(包括如何处理Segmentation): 如何在UICollectionViewCell中设置UILabel

简单的设置

  • 使用故事板
  • 使用Xcode模板创build一个新的集合视图控制器,将其设置为UICollectionViewController的子类。
  • 最初使用默认的UICollectionViewCell,以编程方式添加UILabel。

生成的UICollectionViewController代码在viewDidLoad中注册了单元格:

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

第一个问题: prepareForSegue:发件人:事件不是解雇,这使我回答这个问题。 我实现了UICollectionViewDelegate和collectionView:didSelectItemAtIndexPath:event,然后以编程方式调用了segue。 这确定了我的第一个问题。

第二个问题:我切换到一个包含一个标签的自定义单元格。 把所有东西都挂起来后,单元格标签就不显示了。 经过一番挖掘,我find了解决scheme的顶部链接中的解决scheme。

第三个问题和解决scheme:我删除了registerClass:forCellWithReuseIdentifier:行。 当我运行我的应用程序时,标签显示正确,但是当我点击一个单元格时,它将两次调用prepareForSegue:sender事件。 通过删除registerClass:forCellWithReuseIdentifier行,单元格直接处理单元格触摸,而不需要委托方法。 这是我期望的故事板的工作。 我删除了collectView:didSelectItemAtIndexPath:event,解决了prepareForSegue:sender:的双重触发。 如果您正在使用故事板,请不要注册单元类。 它会覆盖故事板设置的内容。

你是否Triggered Seguesselection Triggered Segues了CollectionView Cell的连接?

你也可以使用[self performSegueWithIdentifier:@"segueIdentifier" sender:nil]以编程方式触发一个segue。

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

用于类似问题的等价Swift代码。

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

请确保,如果您的单元格具有其他重叠视图,则“ 启用用户交互 ”(您可以在属性检查器视图/交互下find此选项)。 否则,您的Tap Gesture被重叠视图占用, didSelectItemAtIndexPath可能不会被调用。