Pau*_*nas 2 uitableview ios uicollectionview uicollectionviewcell
我正在尝试添加一个UILabel给我的collectionViewCell.但是,在一些单元格文本开始与之前的单元格数据重叠之后.
更新:这似乎只发生在我的手机(iphone 5)上,但不在模拟器上(2013 macbook air).
如下所示:

我以编程方式实现整个视图.问题似乎与任何元素没有关系collectionview,所以我认为问题也适用于表格视图.我不确定我是否遗漏了以下内容:
if(cell == nil) {// do something }
Run Code Online (Sandbox Code Playgroud)
如果是这样的话,有人可以解释一下它的作用吗?如果这不是问题,我真的不确定是什么导致了这个问题.我也在使用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;
}
Run Code Online (Sandbox Code Playgroud)
这是我如何设置单元格的大小:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(150, 150);
}
Run Code Online (Sandbox Code Playgroud)
以下是我设置我的方法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];
}
Run Code Online (Sandbox Code Playgroud)
Jay*_*nki 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4703 次 |
| 最近记录: |