gpt4 book ai didi

iphone - UICollectionView:以编程方式查看标题

转载 作者:行者123 更新时间:2023-12-03 18:23:54 26 4
gpt4 key购买 nike

这就是我处理标题的方式。

  //    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"]];
}

最佳答案

您看到的错误非常清楚。您正在尝试使可重用 View 出列,但 Collection View 对您传递的重用标识符一无所知。

您需要在调用 dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: 之前调用 registerClass:forSupplementaryViewOfKind:withReuseIdentifier: 。我通常会调用 viewDidLoad 中的注册方法之一,这样所有设置就完成了,我只需调用出队方法即可。如果您要从 nib 加载自定义页眉和页脚 View ,则可以使用 registerNib:forSupplementaryViewOfKind:withReuseIdentifier: 方法。

编辑

我看到您更新了代码。您需要移动这些行

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

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

在调用寄存器类方法的行上方。在分配和初始化对象之前,您不能向对象发送消息。

关于iphone - UICollectionView:以编程方式查看标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16602227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com