1

I have a view controller (that sits in a tab bar controller), but need to add a collection view to that controller. I see lots of tutorials on google but they all seem to point to creating a UICollectionViewController and starting in viewDidLoad. But how do I do it in a subview?

I have my view controller.m file like so:

- (void) createView // called from viewDidLoad
{

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 64.0, screenWidth, screenHeight)];

    [scrollView.layer addGradient];

    ACollectionView *theview = [[ACollectionView alloc] init];

    [self.view addSubview:theview];
}

Next I started a UICollectionView subclass called ACollectionView.h

@interface ACollectionView : UICollectionView

@end

And the .m file is this:

#import "ACollectionView.h"

@implementation BestCollectionView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

Where do I start the collection view, in the initWithFrame?

I am trying to follow this: Creating a UICollectionView programmatically

Is my paradigm correct?

Community
  • 1
  • 1
cdub
  • 21,144
  • 49
  • 157
  • 274

1 Answers1

0
@property (nonatomic, strong) UICollectionView *collectionView;

- (void)viewDidLoad
{
    [super viewDidLoad];
     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.yourSubview.frame collectionViewLayout:layout];
    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];

    [self.yourSubview addSubview:self.collectionView];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 50;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 100);
}
Suhail kalathil
  • 2,643
  • 1
  • 11
  • 12
  • so i can drop this directly in my base view controller that is tab 1 of my tab bar controller? – cdub Jun 27 '14 at 07:23
  • Coll how do I change the background color to a gradient? I have a CALayer gradient I want to add and have the red dots appear above it? – cdub Jun 27 '14 at 07:41
  • Yea.use this CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = self.collectionView.bounds; gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; [self.collectionView.layer insertSublayer:gradient atIndex:0]; – Suhail kalathil Jun 27 '14 at 08:05