Xib Cell Use in CollectionView

<UICollectionViewDataSource,UICollectionViewDelegate>

VendorListViewController.m

#import “VendorListViewController.h”

#import “NibCell.h”

@interface VendorListViewController ()

@end

@implementation VendorListViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationController.navigationBarHidden=YES;

    UINib *cellNib = [UINib nibWithNibName:@”NibCell” bundle:nil];

    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@”cvCell”];

    

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    [flowLayout setItemSize:CGSizeMake(150, 200)];

    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    [self.collectionView setCollectionViewLayout:flowLayout];

    

    

    // Do any additional setup after loading the view from its nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return 11;

}

-(NibCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    

    static NSString *cellIdentifier = @”cvCell”;

    NibCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    NSLog(@”%ld”,(long)indexPath.row);

    cell.btniblt.tag = (long)indexPath.row;

    return cell;

    

}

#pragma mark collection view cell paddings

– (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    return UIEdgeInsetsMake(0, 20, 10, 20); // top, left, bottom, right

}

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

{

    NSLog(@”%ld”,(long)indexPath.row);

    

}

@end

NibCell.h

#import <UIKit/UIKit.h>

@interface NibCell : UICollectionViewCell

@property (strong, nonatomic) IBOutlet UIButton *btniblt;

– (IBAction)pressButton:(id)sender;

@end

NibCell.m

#import “NibCell.h”

@implementation NibCell

– (void)awakeFromNib {

    // Initialization code

}

– (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

    }

    return self;

}

– (IBAction)pressButton:(id)sender {

    

    NSLog(@”%@”,sender);

    

        NSString *str = [NSString stringWithFormat:@”Button Clicked :: %ld”,(long)_btniblt.tag];

    

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@””

                                                        message:str

                                                       delegate:nil

                                              cancelButtonTitle:@”OK”

                                              otherButtonTitles:nil];

        [alert show];

    NSLog(@”%ld”,(long)_btniblt.tag);

}

@end

Download Sample Project From Github

Leave a comment