UICollectionView单元格未作为UICollectionView的子视图添加

时间:2014-01-08 11:29:44

标签: ios objective-c xcode uicollectionview uicollectionviewcell

我是UICollectionView的新手。

我已关注this tutorial了解如何使用UICollectionView,但问题是在.xib(不使用故事板)中我无法添加UICollectionViewCell }作为UICollectionView的子视图。

在教程中,单元格会在UICollectionView中的.xib下自动创建。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:4)

该教程适用于在xib中使用stroy board工作,您可以创建自定义单元格,首先创建视图控制器,添加UicollectionView以查看控制器

enter image description here enter image description here

然后为集合单元格

创建自定义类

enter image description here

然后抓住空的xib资源并添加集合视图单元格  enter image description here enter image description here enter image description here

并设置该控件的类名和标识符 enter image description here

然后创建委托方法,将自定义单元格添加到你的collectionViewController

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"customCell";

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

    return cell;

}

答案 1 :(得分:0)

你可以启动你的控制器并创建一个自定义集合视图单元来设置你的视图元素,然后你可以将它用到你的集合视图控制器到xib中,这是一个很好的教程来做到这一点http://adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial/

答案 2 :(得分:0)

使用此代码创建集合视图并添加子视图。

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return [imageArray count];
}

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

    NSMutableArray *sectionArray = [imageArray objectAtIndex:section];
    return [sectionArray count];

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    // Setup cell identifier
    static NSString *cellIdentifier = @"cvCell";

    /*  Uncomment this block to use nib-based cells */
    // UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    // UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
    // [titleLabel setText:cellData];
    /* end of nib-based cell block */

    /* Uncomment this block to use subclass-based cells */
    CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    NSMutableArray *data = [imageArray objectAtIndex:indexPath.section];
    NSString *cellData = [data objectAtIndex:indexPath.row];
    [cell.titleLabel setText:cellData];

    cell.imageView.image = [UIImage imageNamed:[imageArray[indexPath.section] objectAtIndex:indexPath.row]];

    return cell;

}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    header *sectionHeader;
    if (kind == UICollectionElementKindSectionHeader)
    {
        sectionHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Hello" forIndexPath:indexPath];
    }
    UICollectionReusableView *test=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Hello" forIndexPath:indexPath];
    return test;

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 3) {
        return CGSizeMake(0, 100);
    }

    return CGSizeZero;
}


- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}


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