ios - 如何在 Collection View 中对单元格单击创建按钮操作

标签 ios objective-c storyboard uicollectionview

实际上我已经使用了两个 Collection View Controller 。在第一个 Collection View Controller 中,我已经传递了图像数组。单击一个图像,我想显示另一个 Collection View Controller 。如何执行此操作...请给我建议。

- (void)viewDidLoad
{
[super viewDidLoad];

recipeImages = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"egg_benedict.jpg", @"full_breakfast.jpg", @"green_tea.jpg", @"ham_and_cheese_panini.jpg", @"ham_and_egg_sandwich.jpg", @"hamburger.jpg", @"instant_noodle_with_egg.jpg", nil];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return recipeImages.count;
}

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

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
for (UIView *view in cell.subviews) {
    if ([view isKindOfClass:[UILabel class]]) {
        [view removeFromSuperview];
    }
}


UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 150, 170)];//Set the frame as per your requirement.
label.font=[UIFont systemFontOfSize:10];

[label setText:[NSString stringWithFormat:@"%@",[categoryArray objectAtIndex:indexPath.row]]];
[cell addSubview:label];
//NSLog(@"hiiiii");


return cell;

最佳答案

如果您想在另一个 View Controller 中显示第二个 Collection View ,只需将包含 Collection View 的 View Controller 推送到 didSelectItemAtIndexPath: 中即可。

如果你想在同一个 View Controller 中显示第二个 Collection View ,你可以创建两个 Collection View 并将第二个设置为隐藏。然后在 didSelectItemAtIndexPath: 中,您可以将第二个 Collection View 设置为显示,将第一个设置为隐藏。这将通过 collectionView.hidden = YES or NO 完成。

关于ios - 如何在 Collection View 中对单元格单击创建按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23880795/

相关文章:

ios - 为什么我在 swift 混合项目成功运行时收到警告?

ios - BST 的 NSDateFormatter 而不是 GMT+1

objective-c - @synchronized 在静态方法中

iphone - 仅在屏幕的有限区域内保持 UIView 可见

iphone - 尝试从 AppDelegate 访问 View Controller 时应用程序崩溃

ios - 如何使用 Storyboard iOS 来布局单个 View 的纵向和横向方向?

ios - 为什么我以编程方式创建的屏幕截图在 iOS 7 上看起来如此糟糕?

ios - GKTurnBasedMatch 不按顺序退出

iphone - 防止 View Controller 被重置 - UINavcontroller + Storyboard + Segue's

ios - 从 Storyboard 设置时看不到 UIImageView 图像