2014-04-01 15 views
22

Mam UICollectionView w jednym z moich viewcontroller. Mój widok kolekcji używa podklasy UICollectionViewLayout (niestandardowej) do rozmieszczenia komórek. Po pierwsze, gdy tylko wybiorę opcję Układ jako niestandardowy w menu Storyboard, opcja wyboru dodatkowych widoków zniknie.Jak programowo dodać nagłówek na UICollectionView z UICollectionViewLayout

Próbowałem robić to programowo, jak pokazano poniżej, ale żadna z metod delegatów nie jest wywoływana.

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{ 
    if (kind == 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 #%li", indexPath.section + 1]; 
     [reusableview addSubview:label]; 
     return reusableview; 
     } 
    return nil; 
    } 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { 
    CGSize headerSize = CGSizeMake(320, 44); 
    return headerSize; 
    } 

W moim metoda viewDidLoad mam

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

Czy ktoś może wskazać mi gdzie mam bałaganu?

+0

masz ustawiony delegata na widok kolekcji? - 'collectionView setDelegate:' - 'collectionView setDataSource:' – james075

+0

Tak, ustawiono źródło danych. Wszystkie inne metody źródeł danych są wywoływane. –

+0

Nie należy dodawać etykiety za każdym razem. Skończysz z etykietami na etykietach. Będzie wymagać podklasy z etykietą jako właściwość lub ustawić ją w instrukcji if, jeśli ma być statyczna. – Sean

Odpowiedz

9

Znaleziono problem, nie wracał atrybuty moim nagłówku w tej metodzie UICollectionLayoutView:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect; // return an array layout attributes instances for all the views in the given rect 
18

Przekazujesz nieprawidłowy widok.

Twoja linia rejestracji klasę:

[self.collectionView registerClass:[UICollectionReusableView class] 
    forSupplementaryViewOfKind:UICollectionElementKindSectionFooter 
    withReuseIdentifier:@"HeaderView"]; 

Powinno być:

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

Edit: Wygląda na to kod to wszystko za pomocą sectionFooter. Czy próbujesz programowo dodać nagłówek lub stopkę?

+0

Próbowałem dodać oba, ale żaden nie działał. Metody delegatów nie są nawet wywoływane. (Zaktualizowałem to pytanie) –

+0

@ManishAhuja Wspomniałeś, że masz niestandardowy układ, czy implementuje layoutAttributesForSupplementaryViewOfKind: atIndexPath:? – dannyzlo

+0

Tak, robi. Ale to także nie jest wywoływane. –

8

Sprawdź, czy dałeś wielkość odniesienia dla nagłówka w UICollectionViewFlowLayout

[flowLayout setHeaderReferenceSize:CGSizeMake(320, 50)]; 

i na stopce

[flowLayout setFooterReferenceSize:CGSizeMake(320, 50)]; 
+5

Te metody są dostępne tylko dla UICollectionViewFlowLayout, a nie dla UICollectionViewLayout. –

0

czek:

if (kind == UICollectionElementKindSectionFooter) 

Twój powinien sprawdzić: UICollectionElementKindSectionHeader

sama dla:

dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter

1

Twoja metoda delegat do wielkości odniesienia nagłówek jest źle, zadzwoń Metoda stopki, referenceSizeForFooterInSection, jak następuje:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { 
    CGSize headerSize = CGSizeMake(320, 44); 
    return headerSize; 
} 

Indywidualnie ustawiona opcja HeaderReferenceSize naprawi problem z nagłówkiem. Ale aplikacja spowoduje awarię, zachowując powyższą metodę i zwracając zero w widoku viewForSuplementaryElementOfKind dla stopki.

1

myślę, że należy nazwać w sposób viewdidload:

[collectionViewFlowLayout setHeaderReferenceSize:CGSizeMake(self.collectionView.frame.size.width, 50)]; 
Powiązane problemy