单元格标签文本在单元格中重叠 [英] Cell Label text overlapping in cells

查看:54
本文介绍了单元格标签文本在单元格中重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am attempting to add a UILabelto my collectionViewCell. However, after a few cell text begins to overlap with data from the cells before.

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

UPDATE: This only seems to happen on my phone (iphone 5), but not on the simulator (2013 macbook air).

如图所示:

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

I am implementing the whole view programmatically. The issue doesn't seem to be with any of the elements of the collectionview, so I would assume the problem applies to table views as well. I am not sure if I am missing something along the lines of:

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

如果是这样,有人可以解释一下这是做什么的吗?如果这不是问题,我真的不确定是什么原因造成的.我也在使用 NSMutableAttributedString,但这也不是问题,因为我试图只插入一个常规字符串并得到相同的结果.

If it is that, can someone shed some light on what this does? If that's not the issue I am really not sure what is causing this. I am also using an NSMutableAttributedString, but that is not the issue either as I tried to insert just a regular string and got the same results.

我的代码:

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

这是我设置单元格大小的方法:

Here is how I set the size of the cells:

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

这里是我如何设置我的 collectionView:

Here is how I setup my 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];
}

推荐答案

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

Remove the label and reinitialize it when cell is display.

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

这篇关于单元格标签文本在单元格中重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆