简体   繁体   English

如何在集合视图中对单元格单击创建按钮操作

[英]How to create button action on cell click in a collection view

Actually I have taken a two collection view controllers. 实际上,我已经采用了两个集合视图控制器。 In first collection view controller iIhave passed array of images.on the click of the one image ,i want to display another collection view controller.how to do this...please suggest me . 在第一个集合视图控制器中,我已传递图像数组。单击一个图像时,我要显示另一个集合视图控制器。如何执行此操作...请建议我。

- (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;

} }

If you would like to show the second collection view in another view controller, you would just push the view controller with the collection view in it in didSelectItemAtIndexPath: . 如果要在另一个视图控制器中显示第二个集合视图,则只需在didSelectItemAtIndexPath:中将包含集合视图的视图控制器推入即可。

If you would like to show the second collection view in the same view controller, you could create two collection views and set the second to hidden. 如果要在同一视图控制器中显示第二个集合视图,则可以创建两个集合视图并将第二个集合视图设置为隐藏。 Then in didSelectItemAtIndexPath: , you could set the second collection view to show and the first to hidden. 然后,在didSelectItemAtIndexPath: ,可以将第二个集合视图设置为显示,将第一个集合视图设置为隐藏。 This would be done through collectionView.hidden = YES or NO. 这可以通过collectionView.hidden = YES or NO.来完成collectionView.hidden = YES or NO.

For displaying another UICollectionViewController on the click of an item in another UICollectionViewController , you have to use the didSelectItemAtIndexPath function. 为了在单击另一个UICollectionViewController中的项目时显示另一个UICollectionViewController ,必须使用didSelectItemAtIndexPath函数。 You can do it like this : 您可以这样做:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    int itemNo = (int)[indexPath row]+1;
    CollectionViewCell *selectedCell = (CollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    switch (itemNo) {
        case 1:
        {
             //perform segue to another UICollectionViewController.
        }
        case 2:
        {
             //perform segue to another UICollectionViewController.
        }
        .
        .
        .
     }
}

Here itemNo is the item(Cell), that is clicked from the Collection View. 这里的itemNo是从“集合”视图中单击的item(Cell)。 If you have to redirect to the same UICollectionViewController for all item clicks, the you won't be needing the switch and you can do it directly with Segue. 如果您必须为所有项目单击重定向到相同的UICollectionViewController ,则无需进行switch ,可以直接使用Segue进行switch

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM