UICollectionView: vue d'en-tête par programme
C'est la façon dont je suis en train d'en-tête.
// 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"]];
}
source d'informationauteur iPhone Programmatically
Vous devez vous connecter pour publier un commentaire.
L'erreur que vous voyez est assez clair. Vous essayez de retirer une réutilisables point de vue, mais l'affichage de la collection ne sais rien à propos de la réutilisation de l'identificateur vous êtes de passage.
Vous devez appeler
registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
avantdequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
est appelé. En général, je composez le registre des méthodes deviewDidLoad
donc l'ensemble de l'installation est fait et je peux juste appeler la résorption de la méthode. Si vous êtes en train de charger votre personnalisé en-tête et pied de page vues de plume, vous pouvez utiliser leregisterNib:forSupplementaryViewOfKind:withReuseIdentifier:
méthode.Modifier
Je vous voir mise à jour du code. Vous devez déplacer ces lignes
au-dessus des lignes où vous appelez le registre des méthodes de la classe. Vous ne pouvez pas envoyer des messages à un objet avant de vous allouer et initialiser.
Si vous n'êtes pas afficher quoi que ce soit sur l'en-tête, comment pourrait-on s'attendre à en-tête de la vue pour vous montrer quelque chose sur la partie en-tête de UICollectionView.Essayez de code suivant.C'est du code qui fonctionne
pourquoi êtes-vous en ajoutant lblHeader de soi.collectionView ajouter à headerView.Et retour headerView.Remplacer votre code avec elle
essayez ceci :
Généralement vous mettre ce registre de la ligne dans le viewDidLoad ou une sorte d'initialiser, de sorte qu'il n'est pas appelé encore et encore. Mais surtout vous avez une certaine confusion. Vous vous inscrivez à une classe pour l'en-tête, mais vous retirer une classe sur le pied de page. Vous devriez vous inscrire à une classe pour tous les deux et retirer le droit pour chacun.
L'avis de votre registre utilise UICollectionElementKindSectionHeader:
L'avis de votre file d'attente utilise UICollectionElementKindSectionFooter :
espère que ça aide.