【问题标题】:UICollectionView header is called only once on the first cellUICollectionView 标头仅在第一个单元格上调用一次
【发布时间】:2015-08-07 02:01:42
【问题描述】:

我现在正在为我的 UICollectionView 实现一个标题,但我现在正在苦苦挣扎,因为标题在第一个单元格上只被调用一次。

这就是我所做的 - 我声明 UICollectionViewFlowLayout 以编程方式创建 UICollectionView - 在本例中为 pickView。然后我添加了两个 xib 文件 - 一个用于单元格,另一个用于标题。我已将这些文件注册到这样的集合视图中:

UICollectionViewFlowLayout *layout2 = [[UICollectionViewFlowLayout alloc] init];

self.pickView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, pointView_y, pointView_width, pointView_length) collectionViewLayout:layout2];
self.pickView.backgroundColor = [UIColor whiteColor];
[self.pickView registerNib:[UINib nibWithNibName:@"TodayCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"TodayCollectionViewCell"];

[self.pickView registerNib:[UINib nibWithNibName:@"PickCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PickCollectionReusableView"];
//layout2.headerReferenceSize = CGSizeMake(_screenWidth, 50);

[self.pickView setDataSource:self];
[self.pickView setDelegate:self];
[self.view addSubview:self.pickView];

然后我这样声明标题视图的大小:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

    return CGSizeMake([Util screenWidth], 50);
}

最后我打电话给viewForSupplementaryElementOfKind,像这样调用头文件:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{


    PickCollectionReusableView *reusableview = (PickCollectionReusableView*) [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PickCollectionReusableView" forIndexPath:indexPath];



    PickCellVariable *buf = (PickCellVariable*) _pickArray[indexPath.row];
    NSLog(@"buf id = %@, SECTION = %lu", buf.itemID, (long)indexPath.row);

    if(buf != nil) {
        //reusableview.thumbPic = ;
        [reusableview.brandName setTitle:[NSString stringWithFormat:@"%lu", (long)indexPath.row] forState:UIControlStateNormal];
        [[reusableview brandName] setBrandName:buf.brandname];
        [[reusableview brandName] addTarget:self action:@selector(brandClick:) forControlEvents:UIControlEventTouchUpInside];

        [[reusableview settingButton] addTarget:self action:@selector(setting:) forControlEvents:UIControlEventTouchUpInside];


        return reusableview;
    }
    else {

        [[reusableview brandName] setBrandName:@""];

        return reusableview;
    }



}

我设置了断点并添加了 NSLog 行,以确保 viewForSupplementaryElementOfKind 在第一个单元格中只被调用一次。

我试图找到像我这样的案例,但是关于 UICollectionView 标头的大多数问题是当他们注册类而不是注册 xib 文件时,或者他们忘记设置标题视图的大小。我的只被调用一次,所以这也与重叠无关。我的收藏视图是以编程方式制作的,所以我现在不重新定义任何东西。

可能是什么问题?拜托,任何事情都可以在这里提供帮助。

谢谢。

【问题讨论】:

  • 你只设置了一个部分吗?如果是这样,viewForSupplementaryElementOfKind 将被调用一次。
  • 好的。这太简单了,让我真的很尴尬。我不敢相信我为此花了将近一天的时间。嗯,吸取了教训。不管怎样,谢谢你这么快的回复!

标签: ios objective-c iphone uicollectionview uicollectionreusableview


【解决方案1】:

好的,所以我的问题是我在row 的基础上调用了我的数据。但是,为了使标题出现在每个单元格上,您必须以section 为基础调用您的数据。 我希望没有人像我一样在这个问题上苦苦挣扎。

【讨论】:

  • 我的问题是headerView.headerTitle.text = categories[indexPath.row].name,必须改成categories[indexPath.section]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多