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?
masz ustawiony delegata na widok kolekcji? - 'collectionView setDelegate:' - 'collectionView setDataSource:' – james075
Tak, ustawiono źródło danych. Wszystkie inne metody źródeł danych są wywoływane. –
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