尝试以编程方式在iOS上创建UICollectionView

时间:2013-11-18 18:19:10

标签: ios cocoa-touch uicollectionview

我们正试图以编程方式为iOS 7制作UICollectionView但是,我们遇到问题,要让细胞显示出来。这是我们的代码。我们知道集合已经完成,numberOfSectionsInCollectionView被调用,numberOfItemsInSection被调用。但是cellForItemAtIndexPath没有或sizeForItemAtIndexPath。 我们在这里缺少什么?

- (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
 */

0 个答案:

没有答案