Add UILabel in UICollectionView cell

I have a UICollectionView that shows images similar to cover art or iBooks. I would like it to show the name of the audio clip under UIImageView. I have a View Controller with a UICollectionView inside it in my MainWindow.xib. I also created NibCell.xib for the cell itself. In the cell, I have a UIImageView that fills everything except the bottom 30 pixels of the cell. In this area, I add UILabel. I give a UIImageView tag of 100 and a UILabel with 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; } 

However, no matter how the cell is configured in NibCell.xib, it causes the UIImageView to fill the entire cell and adds a UILabel on top of it. Any suggestions?

+6
source share
1 answer

You need to add your UILabel to the content content.

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

Source: https://habr.com/ru/post/958506/


All Articles