gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:00 24 4
gpt4 key购买 nike

我正在尝试将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com