Ios 如何在集合视图中的单元格单击上创建按钮操作
实际上,我用了两个集合视图控制器。在第一个集合视图控制器II中,我已经传递了图像数组。单击一个图像,我想显示另一个集合视图控制器。如何执行此操作…请建议我Ios 如何在集合视图中的单元格单击上创建按钮操作,ios,objective-c,storyboard,uicollectionview,Ios,Objective C,Storyboard,Uicollectionview,实际上,我用了两个集合视图控制器。在第一个集合视图控制器II中,我已经传递了图像数组。单击一个图像,我想显示另一个集合视图控制器。如何执行此操作…请建议我 - (void)viewDidLoad { [super viewDidLoad]; recipeImages = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"egg_benedict.jpg", @"full_breakfast.jpg
- (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;
}如果要在另一个视图控制器中显示第二个集合视图,只需在
didSelectItemAtIndexPath:
中推送包含集合视图的视图控制器即可
如果要在同一视图控制器中显示第二个集合视图,可以创建两个集合视图,并将第二个集合视图设置为“隐藏”。然后在
didSelectItemAtIndexPath:
中,可以将第二个集合视图设置为显示,将第一个集合视图设置为隐藏。这可以通过collectionView.hidden=YES或NO来完成。要显示另一个UICollectionViewController
,在另一个UICollectionViewController
中单击一个项目时,必须使用didSelectItemAtIndexPath
功能。您可以这样做:
-(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.
}
.
.
.
}
}
此处itemNo
是从集合视图中单击的项(单元格)。如果所有项目单击都必须重定向到相同的UICollectionViewController
,则不需要使用开关
,您可以直接使用Segue进行操作
希望这有帮助。单击图像后,是否要在不同的视图控制器中显示另一个集合视图控制器?您只需要像您一样推送包含UICollectionView的新视图控制器。