在UICollectionView Cell下添加UILabel [英] Add UILabel Under UICollectionView Cell

查看:89
本文介绍了在UICollectionView Cell下添加UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UICollectionView that shows images, similar to cover art, or iBooks. I would like for it to show the title of the audio clip underneath the UIImageView. I have in my MainWindow.xib a View Controller with a UICollectionView inside it. I also built a NibCell.xib for the cell itself. In the Cell, I have a UIImageView that fills up all but the bottom 30 px of the cell. In this area I add a UILabel. I give the UIImageView a tag of 100 and UILabel a tag of 200, and in my code I put:

-(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。有什么建议吗?

However, no matter how the cell is set up in NibCell.xib, it makes the UIImageView fill the entire cell, and adds the UILabel on top of it. Any suggestions?

推荐答案

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

You need to add your UILabel to cell's contentview

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

这篇关于在UICollectionView Cell下添加UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆