我正在尝试向UILabel
添加collectionViewCell
。但是,在一些单元格文本开始与之前的单元格数据重叠之后。
更新:这似乎只发生在我的手机上(iphone 5),但不在模拟器上(2013 macbook air)。
如下所示:
我正在以编程方式实现整个视图。该问题似乎与collectionview
的任何元素无关,因此我认为问题也适用于表视图。我不确定我是否遗漏了以下内容:
if(cell == nil) {// do something }
如果是这样的话,有人可以说清楚它的作用吗?如果这不是问题,我真的不确定是什么导致了这个问题。我也在使用NSMutableAttributedString
,但这不是问题,因为我试图插入一个常规字符串并获得相同的结果。
我的代码:
- (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];
}
答案 0 :(得分:11)
删除标签并在显示单元格时重新初始化。
- (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;
}
答案 1 :(得分:2)
您只需勾选"清除图形背景"故事板中标签的属性。
答案 2 :(得分:0)
单元格已经包含元素,您还可以通过编程方式创建(添加)标签。
你要做的是:
1.删除UIStoryBoard中UITableViewCell内的UILabel&&以编程方式创建。
- (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;
}
OR
2.在故事板中添加UILabel并使用它的参考&&不要在代码中创建只引用(指向)。
使用标记属性指向元素。
- (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;
}