Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:未触发情节提要集合视图序列_Ios_Uicollectionview_Uistoryboard - Fatal编程技术网

iOS:未触发情节提要集合视图序列

iOS:未触发情节提要集合视图序列,ios,uicollectionview,uistoryboard,Ios,Uicollectionview,Uistoryboard,我在导航控制器中嵌入了UICollectionView控制器。collectionView列出了项目,每个单元格都应该进入一个ProjectDetail屏幕 我根本无法触发这个序列。如果我只需在导航栏上按下一个按钮,然后连接一段细节,它就可以工作了。但从我的CollectionView手机触发不会 故事板是这样的:我确实有一个从CollectionViewCell连接到ProjectDetailViewController的segue 以下是我的ProjectDetailViewControll

我在导航控制器中嵌入了UICollectionView控制器。collectionView列出了项目,每个单元格都应该进入一个ProjectDetail屏幕

我根本无法触发这个序列。如果我只需在导航栏上按下一个按钮,然后连接一段细节,它就可以工作了。但从我的CollectionView手机触发不会

故事板是这样的:我确实有一个从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, 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


任何帮助都将不胜感激

您是否在
选择
上的
触发序列
中建立了CollectionView单元格的连接

您还可以使用编程方式触发一个segue
[self-PerformsgueWithIdentifier:@“segueIdentifier”发送方:无]


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

由于collectionView是通过数据源动态填充的,因此无法直接从情节提要中的单元格创建分段。您应该使用并使用编程方式执行segue。大概是这样的:

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

其中,
MySegueIdentifier
是序列图像板中定义的序列的标识符。

TLDR:对于序列图像板,不要调用registerClass:forCellWithReuseIdentifier:。它覆盖故事板为单元设置的内容(包括如何处理分段):

简要设置

  • 使用故事板
  • 使用Xcode模板创建了新的集合视图控制器, 将其设置为UICollectionViewController的子类
  • 最初使用默认的UICollectionViewCell,添加UILabel 以编程方式
生成的UICollectionViewController代码在viewDidLoad中注册了单元格:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
第一期: prepareForSegue:sender:事件没有触发,这让我得到了这个答案。 我实现了UICollectionViewDelegate和collectionView:didSelectItemAtIndexPath:事件,然后以编程方式调用segue。 这解决了我的第一个问题

第二期:我切换到包含一个标签的自定义单元格。在连接完所有内容后,单元格标签没有显示出来。 经过一番挖掘,我在答案顶部的链接中找到了一个解决方案


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

类似问题的等效Swift代码

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

确保,如果您的单元格有其他重叠视图,“已启用用户交互”未选中(您可以在属性检查器视图/交互下找到此选项)。否则,您的点击手势将被重叠视图占用,didSelectItemAtIndexPath可能无法调用。

有人能告诉我发生了什么变化吗?因为在这里我们可以清楚地看到,这个连接是在
故事板中建立的,并且是有效的。这个答案已经过时了。当然,您可以直接在情节提要中创建分段。@克劳德您知道为什么使用collectionView:didSelectItemAtIndePath:覆盖情节提要设置的内容吗?