I want to create a grid with buttons. Most of the sample applications I've seen explain how to create an image grid, but none of them explain for buttons.
How to set a button in a cell UICollectionView
?
Here is my code
FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectView;
@end
FirstViewController.m
#import "FirstViewController.h"
#import "CustomCell.h"
@interface FirstViewController ()
{
NSArray *arrayOfBtns;
}
@end
@implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[self myCollectView]setDelegate:self];
[[self myCollectView]setDataSource:self];
arrayOfBtns=[[NSArray alloc]initWithObjects:@"Save",@"Goto", nil];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayOfBtns count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
CustomCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIButton *btn;
@end
CustomCell.m
#import "CustomCell.h"
@implementation CustomCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (IBAction)btn:(id)sender {
}
@end
source
share