UICollectionView:以编程方式创建头视图

14

这是我正在处理标题的方式。

  //    viewDidLoad

  [self.collectionView registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:@"Cell"];

[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

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

self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 390, 300) collectionViewLayout:layout];
layout.collectionView.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
[self.collectionView registerClass:[UICollectionViewCell class]
        forCellWithReuseIdentifier:@"Cell"];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
layout.minimumInteritemSpacing = 15;

[layout setScrollDirection:UICollectionViewScrollDirectionVertical];

self.collectionView.collectionViewLayout = layout;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.showsVerticalScrollIndicator = NO;
[self.collectionView setBounces:YES];
self.collectionView.allowsMultipleSelection = YES;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"HorizontalPickerCell"];
[self.view addSubview:self.collectionView];

   - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  {
  if (section == 0) {
    return CGSizeMake(0, kHeaderHeight);
  }
  return CGSizeMake(0, kHeaderHeight + kInterSectionMargin);
  }

  - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
  {
NSLog(@"levels count:%d", [arrLevels count]);
return [arrLevels count];
  }

  - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
 {
 NSLog(@"vals count:%d", [arrSeats count]);
 for(int i=0; i<[arrLevels count]; i++)
 {
    if(section == i)
    {
        int c;
        NSString *cnt = [NSString stringWithFormat:@"%@",[arrTot objectAtIndex:i]];
        c = [cnt intValue];
        return c;
    }
 }
 return 1;
 }

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

//cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue_s.png"]];

if (cell.selected) {
    [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_s.png"]]; // highlight selection
 }
else
 {
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow_seat.png"]]; // Default color
 }

return cell;
 }

 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 12, 10, 10);
 }

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:  (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize retval =  CGSizeMake(22, 20);
return retval;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;

if (kind == UICollectionElementKindSectionHeader) {
    UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:nil forIndexPath:indexPath];

        self.lblHeader = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 100, 30)];
        self.lblHeader.backgroundColor = [UIColor whiteColor];
        self.lblHeader.textColor = [UIColor redColor];
        self.lblHeader.text = @"Level 1";
        [self.collectionView addSubview:self.lblHeader];
}
return reusableview;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
printf("Selected View index=%d",indexPath.row);

itemPaths  = [self.collectionView indexPathsForSelectedItems];

UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_s.png"]];
 }

 - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
 {
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow_seat.png"]];
 }
5个回答

15
你看到的错误非常明显。你正在尝试取消排队可重用视图,但集合视图不知道你传递的重用标识符。
在调用dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:之前,需要调用registerClass:forSupplementaryViewOfKind:withReuseIdentifier:。我通常在viewDidLoad中调用其中一个注册方法,这样所有设置都完成了,我只需要调用取消排队的方法。如果你从nib加载自定义页眉和页脚视图,你可以使用registerNib:forSupplementaryViewOfKind:withReuseIdentifier: 方法。

编辑

我看到你更新了代码。你需要将这些行移到下面:
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];

self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 390, 300) collectionViewLayout:layout];

在调用注册类方法的那些行之前,您不能向对象发送消息。您必须先分配并初始化它。


那件事情已经完成,请在问题的最后一行检查。 - iPhone Programmatically
是的,你在最后一行调用了它。在调用dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:之前需要调用它。registerClass:forSupplementaryViewOfKind:withReuseIdentifier:不应该从这个方法中调用。把它移到viewDidLoad或者在你配置视图的地方。 - CEarwood
还有你发布的错误提到了“Test Header View”,我猜这是来自另一个答案?确保你的 reuseIdentifier 是相同的。这样 collectionview 才知道你正在出列哪种类型的可重用视图。 - CEarwood
不,那不是问题,那是我的发布错误。我已经编辑了问题。 - iPhone Programmatically
就像我说的那样,错误非常明显。在调用dequeue方法之前,您没有注册类。如果没有看到更多的代码,我无法告诉您错误出现在哪里。在从此问题中复制和粘贴更新的代码后,我无法复制您的问题。您现在有3个答案,根据我们拥有的信息,所有3个答案都是正确的。 - CEarwood
显示剩余2条评论

12

如果您在页眉上没有显示任何内容,那么您如何期望页眉视图在UICollectionView的页眉部分显示内容呢?请尝试以下代码。这是有效的代码。

- (void)viewDidLoad
{
    arry=[[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four", nil];
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

    // Do any additional setup after loading the view, typically from a nib.
}


    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [arry count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 20;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    if (cell==nil) {
        cell=[[UICollectionViewCell alloc] init];
    }

    UILabel *label = (UILabel*)[cell.contentView viewWithTag:21];

    if (label==nil) {
      label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
      label.tag = 21;
      [cell.contentView addSubview:label];
    } 

    label.text = [arry objectAtIndex:indexPath.row];

    NSLog(@"cell is %@",NSStringFromCGRect(cell.frame));
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(60, 60);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    UIEdgeInsets insets=UIEdgeInsetsMake(10, 10, 10, 10);
    return insets;
}

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

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

        if (reusableview==nil) {
            reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        }

        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        label.text=[NSString stringWithFormat:@"Recipe Group #%i", indexPath.section + 1];
        [reusableview addSubview:label];
        return reusableview;
    }
    return nil;
}

我已经更新了我的代码,请检查。 - iPhone Programmatically
5
首先,你不需要整个"reusableView == nil"语句——它永远不会为nil,这就是向collectionView注册可重用视图类的全部含义。其次,你可能会得到一个已经使用过的视图,它已经有一个标签,所以一定要从可重用视图中删除所有子视图......像这样:[reusableView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; - n13
在 viewForSupplementaryElementOfKind 中,UILabel 的作用域是什么?@aeu,你能告诉我 UILabel 是在哪里以及如何泄漏的吗? - Prince Kumar Sharma
@Vive 你是对的,字符串应该使用字符串方法进行比较。我已经更新了我的答案。 - Prince Kumar Sharma
当您将标签添加到内容视图中时,它会被内容视图保留。然后,您需要在分配的原始标签上调用“release”-因此,在具有 [cell.contentView addSubview:label] 行之后,您需要使用 [label release]。请参考Apple关于内存管理的文档以获取更多信息。 - aeu
显示剩余5条评论

4
为什么要将lblHeader添加到self.collectionView的headerView中?并返回headerView。请使用以下代码替换您的代码:
    if (kind == UICollectionElementKindSectionHeader) {
    UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:nil forIndexPath:indexPath];

        self.lblHeader = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 100, 30)];
        self.lblHeader.backgroundColor = [UIColor whiteColor];
        self.lblHeader.textColor = [UIColor redColor];
        self.lblHeader.text = @"Level 1";

       // [self.collectionView addSubview:self.lblHeader];
          [headerView addSubview:self.lblHeader];

       return headerView;
}

2
通常将此注册行放置在viewDidLoad或某种初始化中,以便不会反复调用。但主要问题是您有些困惑。您为标题注册一个类,但在页脚上取消排队一个类。您应该为两个都注册一个类,并为每个正确地取消排队。
请注意,您的注册使用UICollectionElementKindSectionHeader:
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

请注意,您的dequeue使用了UICollectionElementKindSectionFooter:

UICollectionReusableView *supplementaryView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"header" forIndexPath:indexPath];

希望这有所帮助。

不,那不是问题,那是我的发布错误。我编辑了问题。 - iPhone Programmatically
你当前的错误是什么?那已经从问题中删除了。 - Aditya Nagrath
它只是没有显示出来吗?如果我没记错,你告诉它单元格的宽度为0。 - Aditya Nagrath
谢谢你给了我关于框架的想法。 - iPhone Programmatically
我在滚动时选择的洗牌问题上遇到了问题,你能帮忙吗?这是链接:“http://stackoverflow.com/questions/16662363/selecting-multiple-values-shuffle-when-scrolled-uicollectionview”。 - iPhone Programmatically
这是我的情况 :) - Vitya Shurapov

1

try this :

    //ON top 
    static NSString *headerViewIdentifier = @"Test Header View";
    static NSString *footerViewIdentifier = @"Test Footer View";


    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        NSString *identifier = nil;

        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            identifier = headerViewIdentifier;
        }
        else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
            identifier = footerViewIdentifier;
        }

        // TODO Setup view

        UICollectionReusableView *supplementaryView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:identifier forIndexPath:indexPath];

        return supplementaryView;
    }

但是你还没有注册这个类,所以它会报错。 - iPhone Programmatically

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接