在UICollectionView Cell下添加UILabel

时间:2013-11-20 19:13:15

标签: ios uiimageview uilabel uicollectionview uicollectionviewcell

我有一个显示图像的UICollectionView,类似于封面图或iBooks。我希望它能在UIImageView下面显示音频剪辑的标题。我在我的MainWindow.xib中有一个视图控制器,里面有一个UICollectionView。我还为单元格本身构建了一个NibCell.xib。在Cell中,我有一个UIImageView,它可以填充除了底部30像素的细胞之外的所有细胞。在这个区域我添加了一个UILabel。我给UIImageView标签100和UILabel标签200,在我的代码中我把:

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

     RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];

    static NSString *cellIdentifier = @"cvCell";

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

    UIImageView *titleLabel = (UIImageView *)[cell viewWithTag:100];
     UILabel *titleLabel2 = (UILabel *)[cell viewWithTag:200];

           NSString *thearticleImage = entry.articleImage;
               [titleLabel setImageWithURL:[NSURL URLWithString:entry.articleImage] placeholderImage:[UIImage imageNamed:@"icon@2x.png"]];

        [titleLabel2 setText:entry.articleTitle];
        return cell;
}

但是,无论在NibCell.xib中如何设置单元格,都会使UIImageView填充整个单元格,并在其上添加UILabel。有什么建议吗?

1 个答案:

答案 0 :(得分:12)

您需要将UILabel添加到单元格的内容视图

UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
title.tag = 200;
[cell.contentView addSubview:title];