无法看到我的UICollectionViewCells

时间:2013-04-14 21:35:56

标签: ios uicollectionview uicollectionviewcell

我正在使用UICollectionViewController编写应用程序。

我用故事板创建了布局。 Subclassed UICollectionViewController并在IB中链接两个。我还给它一个Storyboard ID" ImageCollectionViewController"。

我还将UICollectionViewCell子类化,并将其与IB中的原型单元链接,并将其标识为“ImageCell"”。

这是我调用创建自定义UICollectionViewController的构造函数:

-(id)initWithGroup:(CatalogGroup*)group
{
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle: nil];
    self = [storyboard instantiateViewControllerWithIdentifier:@"ImageCollectionViewController"];
    if (self) {

        [self setGroup:group];

        // Register the type of cell for collection view
        [self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
    }

    return self;
}

现在这一切似乎都有效。所有委托方法都在collectionViewController中调用。例如:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self fetchedResultsController] sections][section];
    return [sectionInfo numberOfObjects];
}

我的细胞正在初始化并传回:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* identifier = @"ImageCell";
    ImageCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    ImageItem* item = [[self fetchedResultsController].fetchedObjects objectAtIndex:indexPath.row];

    if(cell) {
        // Set up label
        cell.label.lineBreakMode = NSLineBreakByCharWrapping;
        cell.label.numberOfLines = 0;
        [cell.label setText:item.labelText];

        NSData* imageData = item.imageData;
        UIImage* image = [UIImage imageWithData:imageData];
        UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
        [cell setImageView:imageView];
    }

    return cell;
}

然而,当我将视图控制器推到堆栈上时。没有细胞在那里? 我的堆栈有什么问题吗?

我在IB中为CollectionView设置的背景颜色正在显示,所以我相信它正在被正确创建。细胞被初始化。正在调用委托方法,所以我不知道从哪里开始调试。

2 个答案:

答案 0 :(得分:0)

我不确定你的自定义单元格是什么样的,但一般来说,它应该是cell.contentView的子视图:

[cell.contentView addSubview:myContentView];

因此,您可以尝试

而不是[cell setImageView:imageView]
[cell.contentView addSubview:imageView];

答案 1 :(得分:0)

您可以通过在集合视图单元格自定义类中设置单元格的背景颜色来测试集合视图。这将确保您的单元格配置正确。现在来解决你的问题(这只是猜测,因为你没有显示你的细胞类)。如果要在nib中设置单元格的图像视图,则应该为单元格_ registerNib 而不是_ registerClass _。另外,您应该将图像视图作为子视图添加到cell.contentView

相关问题