ios - 在 UICollectionView 单元格下添加 UILabel

标签 ios uiimageview uilabel uicollectionview uicollectionviewcell

我有一个显示图像的 UICollectionView,类似于封面或 iBooks。我希望它在 UIImageView 下方显示音频剪辑的标题。我在我的 MainWindow.xib 中有一个 View Controller ,里面有一个 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。有什么建议吗?

最佳答案

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

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

关于ios - 在 UICollectionView 单元格下添加 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20104840/

相关文章:

c# - 在 ASP.net 中从 Swift/iOS 客户端请求接收 POST 数据

ios - UITableViewCell 中的 UIImage 正在调整行选择和从推送 View 返回时的大小

ios - 关于 iOS 中的 setUserInteractionEnabled UIImageView

ios - 如何快速制作不同形状的 ImageView

ios7 与 uilabel 中的 nstextattachment 的自定义交互

ios - 带有变体的属性 UILabel 在应用程序发送到后台后重置文本

iphone - 用于数据的文本文件

ios - 当表格 View 向上滚动时,表格 View 第一部分(第 0 部分)没有向上移动

ios - 如何使动态高度 UILabel 将其他元素推开?

ios - 通过 Pods 将 SocketRocket 添加到 Swift 项目