2015-05-08 5 views
1

Мой заголовок в моем UICollectionViews, кажется, удваивается при прокрутке. Заголовок 1 прокручивается вверх -> Заголовок 2 теперь находится в позиции заголовка 1. прокрутки вниз и заголовок 1 теперь показывает заголовок 1 и заголовок 2. Вот мой код:Ошибка заголовка UICollectionView, заголовки удваиваются

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{ 
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { 

    UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath]; 


    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(134, 2, 53, 16)]; 
    label.font = [UIFont fontWithName:@"HelveticaNeue" size:10]; 
    label.text = [NSString stringWithFormat:@"Hole %i", indexPath.section + 1]; 

    [reusableview addSubview:label]; 
    return reusableview; 
} 

return nil ; 
} 

image of issue

Я понятия не имею, как решить эту проблему.

+0

Вы были в состоянии исправить этот вопрос – Nassif

ответ

0

Вы можете попробовать это

UILabel *_label; 
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{ 
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { 

    UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath]; 

    if(_label) 
    { 
     [_label removeFromSuperView]; 
    } 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(134, 2, 53, 16)]; 
    label.font = [UIFont fontWithName:@"HelveticaNeue" size:10]; 
    label.text = [NSString stringWithFormat:@"Hole %i", indexPath.section + 1]; 

    [reusableview addSubview:label]; 
    _label=label; 
    return reusableview; 
} 

return nil ; 
} 
+0

Сортировка работ, верхние и нижние заголовки теперь появляются нормальные, но все заголовки между ними пустые. – CrazyGoose

0

Попробуйте этот образец

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView 
     viewForSupplementaryElementOfKind:(NSString *)kind 
          atIndexPath:(NSIndexPath *)indexPath { 
// You can make header an ivar so we only ever create one 
if (!header) { 

    header = [collectionView dequeueReusableSupplementaryViewOfKind:kind 
               withReuseIdentifier:@"ident" 
                 forIndexPath:indexPath]; 
    CGRect bounds; 
    bounds = [header bounds]; 

    UIImageView *imageView; 
    imageView = [[UIImageView alloc] initWithFrame:bounds]; 
    [imageView setImage:[UIImage imageNamed:@"header-background"]]; 

    // Make sure the contentMode is set to scale proportionally 
    [imageView setContentMode:UIViewContentModeScaleAspectFill]; 
    // Clip the parts of the image that are not in frame 
    [imageView setClipsToBounds:YES]; 
    // Set the autoresizingMask to always be the same height as the header 
    [imageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; 
    // Add the image to our header 
    [header addSubview:imageView]; 
} 
return header; 
} 
-1
[reusableView makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
+0

Куда это уходит? – CrazyGoose

+0

Отсутствие видимого интерфейса объявляет селектор ..... – CrazyGoose

+0

Прошу прощения. Попробуйте повторно использовать View.subviews, а не просто повторно использовать. Дело в том, чтобы избавиться от subviews в reusableView, поскольку они содержат оставленные виды из предыдущего прокрутки прокрутки. – RegularExpression

Смежные вопросы