单元格中的单元格标签文本重叠

我正在尝试添加一个UILabel到我的collectionViewCell 。 但是,一些单元格文本开始与之前的单元格数据重叠之后。

更新 :这似乎只发生在我的手机(iphone 5),但不是在模拟器(2013年macbook空气)。

如这里所见:

在这里输入图像说明

我正在以编程方式实现整个视图。 这个问题似乎并没有与任何collectionview的元素,所以我会认为这个问题也适用于表视图。 我不确定我是否错过了以下几点:

 if(cell == nil) {// do something } 

如果是这样的话,有人可以点亮一下这个function吗? 如果这不是问题,我真的不知道是什么原因造成的。 我也使用一个NSMutableAttributedString ,但是这不是问题,因为我试图插入一个普通的string,并得到相同的结果。

我的代码:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; TTSong *tempSong = [songArray objectAtIndex:indexPath.row]; UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 110, 150, 40)]; [cellLabel setTextColor:[UIColor whiteColor]]; [cellLabel setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]]; [cellLabel setFont:[UIFont fontWithName: @"HelveticaNeue-Light" size: 12.0f]]; [cellLabel setNumberOfLines:2]; NSString * labelString = [[tempSong.artist stringByAppendingString:@"\n"] stringByAppendingString:tempSong.title]; NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:labelString]; NSRange boldedRange = NSMakeRange(0, tempSong.artist.length); [attributedString addAttribute: NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:14.0f] range:boldedRange]; [cellLabel setAttributedText:attributedString]; [cell addSubview:cellLabel]; return cell; } 

这里是我如何设置单元格的大小:

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(150, 150); } 

这里是我如何设置我的collectionView

 - (void)viewDidLoad { songArray = [[NSMutableArray alloc] init]; UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; _collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout]; [_collectionView setDataSource:self]; [_collectionView setDelegate:self]; [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; [_collectionView setBackgroundColor:[UIColor blackColor]]; [self.view addSubview:_collectionView]; [super viewDidLoad]; } 

显示单元格时移除标签并重新初始化它。

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; TTSong *tempSong = [songArray objectAtIndex:indexPath.row]; for (UILabel *lbl in cell.contentView.subviews) { if ([lbl isKindOfClass:[UILabel class]]) { [lbl removeFromSuperview]; } } UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 110, 150, 40)]; [cellLabel setTextColor:[UIColor whiteColor]]; [cellLabel setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]]; [cellLabel setFont:[UIFont fontWithName: @"HelveticaNeue-Light" size: 12.0f]]; [cellLabel setNumberOfLines:2]; NSString * labelString = [[tempSong.artist stringByAppendingString:@"\n"] stringByAppendingString:tempSong.title]; NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:labelString]; NSRange boldedRange = NSMakeRange(0, tempSong.artist.length); [attributedString addAttribute: NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:14.0f] range:boldedRange]; [cellLabel setAttributedText:attributedString]; [cell addSubview:cellLabel]; return cell; } 

你只需要勾选故事板中标签的“清除graphics上下文”属性。

单元格已经具有这些元素,并且您还可以通过编程创build(添加)标签。

你必须做的是:

1.在UITtoryBoard中的UITableViewCell中删除UILabel &&以编程方式创build。

  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; TTSong *tempSong = [songArray objectAtIndex:indexPath.row]; UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 110, 150, 40)]; [cellLabel setTextColor:[UIColor whiteColor]]; [cellLabel setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]]; [cellLabel setFont:[UIFont fontWithName: @"HelveticaNeue-Light" size: 12.0f]]; [cellLabel setNumberOfLines:2]; NSString * labelString = [[tempSong.artist stringByAppendingString:@"\n"] stringByAppendingString:tempSong.title]; NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:labelString]; NSRange boldedRange = NSMakeRange(0, tempSong.artist.length); [attributedString addAttribute: NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:14.0f] range:boldedRange]; [cellLabel setAttributedText:attributedString]; [cell addSubview:cellLabel]; return cell; } 

要么

2.在故事板中添加UILabel,并使用它的引用&&不要在代码中创build只是引用(指向)它

使用Tag属性指向元素。

  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; TTSong *tempSong = [songArray objectAtIndex:indexPath.row]; UILabel *cellLabel = (UILabel*)[cell ViewWithTag:25]; [cellLabel setTextColor:[UIColor whiteColor]]; [cellLabel setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]]; [cellLabel setFont:[UIFont fontWithName: @"HelveticaNeue-Light" size: 12.0f]]; [cellLabel setNumberOfLines:2]; NSString * labelString = [[tempSong.artist stringByAppendingString:@"\n"] stringByAppendingString:tempSong.title]; NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:labelString]; NSRange boldedRange = NSMakeRange(0, tempSong.artist.length); [attributedString addAttribute: NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:14.0f] range:boldedRange]; [cellLabel setAttributedText:attributedString]; // [cell addSubview:cellLabel]; Don't add this again return cell; }