UICollectionView标题问题,标题加倍

时间:2015-05-08 18:39:51

标签: objective-c uicollectionview

我的UICollectionViews中的标题视图在滚动时似乎加倍。 标题1向上滚动 - >标题2现在位于标题1位置。 向下滚动,标题1现在显示标题1和标题2.这是我的代码:

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

我不知道如何解决这个问题。

3 个答案:

答案 0 :(得分: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 ;
}

答案 1 :(得分: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;
}

答案 2 :(得分:-1)

[reusableView makeObjectsPerformSelector:@selector(removeFromSuperview)];