2014-02-19 176 views
2

我正在嘗試將UILabel添加到我的collectionViewCell。但是,一些單元格文本開始與之前的單元格數據重疊之後。在單元格中重疊的單元格標籤文本

更新:這似乎只發生在我的手機(iphone 5),但不是在模擬器(2013年macbook空氣)。

正如在這裏看到:

enter image description here

我以編程方式實現整體視圖。這個問題似乎沒有與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]; 
} 
+2

單元格被重複使用,並且您的代碼會將另一個單元格標籤添加到已有單元格的重用單元格中。如果你打算以這種方式在代碼中添加標籤,那麼在添加另一個之前,你需要檢查該單元格是否已經有一個。 – rdelmar

+0

謝謝。所以如果這個單元已經有了標籤,我需要先清除它? 有沒有更好的方法來實現這個?你能從該代碼中發現更多不好的做法嗎? – user1933131

+1

你可以清除它,或者只是檢查是否有一個,如果有的話不要添加另一個。其他的方法是在故事板中創建帶有標籤的單元格(這是最簡單的方法),或者創建一個自定義類,然後在init方法中添加標籤。 – rdelmar

回答

9

移除標籤並在顯示單元格時重新初始化它。

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

我結束了使用標籤去除標籤。 – user1933131

+0

好主意!謝謝:) –

+1

任何人請幫助迅速版本... –

0

細胞已經有元素,還可以通過編程創建(添加)標籤。

你所要做的是:

1卸下襬臂在UIStoryBoard & &中的UITableViewCell內的UILabels創建編程。

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

您只需勾選故事板中標籤的「清除圖形上下文」屬性即可。

+0

這是適當的,我想知道爲什麼downvoted。 –

相關問題