Home:ALL Converter>Trying to make a UICollectionView programmatically iOS

Trying to make a UICollectionView programmatically iOS

Ask Time:2013-11-19T02:19:10         Author:Michael Choi

Json Formatter

We are trying to make a UICollectionView for iOS 7 programmatically however, we are running into problems for the cells to show up. Here is our code. We know that the collection gets made and the numberOfSectionsInCollectionView gets called and the numberOfItemsInSection gets called. However the cellForItemAtIndexPath does not or the sizeForItemAtIndexPath. What are we missing here?

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

- (void) setUpCollectionView
{
    collectionViewLayout = [[UICollectionViewLayout  alloc] init];
    teamCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout: collectionViewLayout];
    [teamCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier: @"cellIdentifier"];
    teamCollectionView.dataSource = self;
    teamCollectionView.delegate = self;


    teamCollectionView.backgroundColor = [UIColor yellowColor];
    [self addSubview: teamCollectionView];

}

/**
 * STARTS THE COLLECTION VIEW FOR THE VIEW
 */

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath: indexPath];

    cell.backgroundColor = [UIColor greenColor];

    return cell;
}

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

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

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(50, 50);
}

/**
 * ENDS THE COLLECTION VIEW FOR THE VIEW
 */

Author:Michael Choi,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/20054910/trying-to-make-a-uicollectionview-programmatically-ios
yy