0

I'm new to iOS development. I need to use a CollectionView without using Storyboard and this view has to be load in a tab. Is this possible.

Thanks

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Gayan
  • 185
  • 1
  • 4
  • 14

2 Answers2

0

Ya. you can create it programmatically

-(void)loadView
{
 [super loadView];

 UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
 self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds  collectionViewLayout:layout];
 self.collectionView.delegate=self;
 self.collectionView.dataSource=self;
 [self.collectionView setAutoresizingMask:UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];

 [self.collectionView setBackgroundColor:[UIColor clearColor]];
 [self.collectionView registerClass:[UICollectionViewCell class]
    forCellWithReuseIdentifier:@"Cell"];


[self.view addSubView:self.collectionView];
}
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
0

You have to create a UIViewController subclass that implements UICollectionViewDataSource and UICollectionViewDelegate like this:

@interface MyViewController:UIViewController<UICollectionViewDelegate, UICollectionViewDataSource>
@end

then in the .m file you implement the required UICollectionViewDataSource and UICollectionViewDelegate Methods

and override -[UIViewController viewDidLoad] like so:

- (void)viewDidLoad {
  [super viewDidLoad];
  UICollectionView *collectionView = [UICollectionView alloc] initWithFrame:self.view.bounds];
  collectionView.delegate = self;
  collectionView.dataSource = self;
  [self.view addSubview:collectionView];
}
Nick C
  • 645
  • 3
  • 6
  • See more here http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UICollectionView_class/Reference/Reference.html – Nick C Mar 15 '13 at 05:15