Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Azerix

macrumors newbie
Original poster
May 4, 2013
7
0
Amsterdam
Hi,

I have a collectionView and i want to add buttons inside the cells. Each cell has its own button. This is what i have:

CustomMenuCell.h

Code:
@property (weak, nonatomic) IBOutlet UILabel *menuDescriptionLabel;

@property (weak, nonatomic) IBOutlet UIButton *button;


MenuViewController.h
Code:
@property (strong, nonatomic) IBOutlet UICollectionView *menuCollectionView;

-(void)buttonAction:(UIButton*)button;

MenuViewController.m file:
Code:
#import "MenuViewController.h"
#import "CustomMenuCell.h"
#import "DetailViewController.h"

@interface MenuViewController ()
{
    NSArray *arrayMenuDescription;
    NSMutableArray *button;
}

@end

@implementation MenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[self menuCollectionView]setDataSource:self];
    [[self menuCollectionView]setDelegate:self];
   
    arrayMenuDescription = [[NSArray alloc]initWithObjects:@"One",@"Two", nil];
    button = [[NSMutableArray alloc]init];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [arrayMenuDescription count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:
    (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    [[cell button] addTarget:self action:@selector(buttonAction:event:) forControlEvents:UIControlEventTouchUpInside];
    
    [[cell menuDescriptionLabel]setText:[arrayMenuDescription objectAtIndex:indexPath.item]];
    return cell;
}
//Button action performed
-(void)buttonAction:(UIButton*)button {
    NSLog(@"test");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

So far i have accomplished to generate cell with Labels and one button. But i want to achieve to make array of buttons. And call each button separate, fore example :
Code:
[self.arrayButtons objectAtIndex:2];
so that i can decorate this object separately.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.