ios - 单元格标签文本在单元格中重叠

标签 ios uitableview uicollectionview uicollectionviewcell

我正在尝试将 UILabel 添加到我的 collectionViewCell。然而,一些单元格文本开始与之前单元格中的数据重叠。

更新:这似乎只发生在我的手机 (iphone 5) 上,但不会发生在模拟器 (2013 macbook air) 上。

如图所示:

enter image description here

我正在以编程方式实现整个 View 。该问题似乎与 collectionview 的任何元素无关,因此我认为该问题也适用于 TableView 。我不确定我是否遗漏了以下内容:

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];
}

最佳答案

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

- (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;
}

关于ios - 单元格标签文本在单元格中重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871495/

相关文章:

objective-c - 结合 UIWindows?

ios - 如何使用 nib 文件为 TableView 创建动态单元格

ios - UICollectionView ios 7 问题

ios - 在使用转换时替换 UIWindow 的 rootViewController,似乎正在泄漏

ios - 内存不断增长,但没有泄漏报告

ios - scrollView 底部内容不响应触摸事件

ios - 删除 CoreData 对象

ios - 向 UITableViewCell 添加了自动布局,但我看到了未知现象

ios - 在 collectionView 的 indexPath.row 获取 UILabel.text

ios - UICollectionView 在重新加载时更改项目顺序