Нахавандипур Вандад
Шрифт:
self = [super initWithCollectionViewLayout: layout];
if (self!= nil){
/* Регистрируем nib-файл со сборным видом для удобного получения */
UINib *nib = [UINib nibWithNibName:
NSStringFromClass([MyCollectionViewCell class])
bundle: [NSBundle mainBundle]];
[self.collectionView registerNib: nib
forCellWithReuseIdentifier: kCollectionViewCellIdentifier];
/* Регистрируем nib-файл верхнего колонтитула */
UINib *headerNib = [UINib
nibWithNibName: NSStringFromClass([Header class])
bundle: [NSBundle mainBundle]];
[self.collectionView registerNib: headerNib
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader
withReuseIdentifier: kCollectionViewHeaderIdentifier];
/* Регистрируем nib-файл нижнего колонтитула */
UINib *footerNib = [UINib
nibWithNibName: NSStringFromClass([Footer class])
bundle: [NSBundle mainBundle]];
[self.collectionView registerNib: footerNib
forSupplementaryViewOfKind: UICollectionElementKindSectionFooter
withReuseIdentifier: kCollectionViewFooterIdentifier];
}
return self;
}
Переходим к реализации метода collectionView: viewForSupplemen taryElementOfKind: atIndexPath: сборного вида. Этот метод нужен нам для конфигурирования верхних и нижних колонтитулов и предоставления их обратно сборному виду:
— (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath{
NSString *reuseIdentifier = kCollectionViewHeaderIdentifier;
if ([kind isEqualToString: UICollectionElementKindSectionFooter]){
reuseIdentifier = kCollectionViewFooterIdentifier;
}
UICollectionReusableView *view =
[collectionView dequeueReusableSupplementaryViewOfKind: kind
withReuseIdentifier: reuseIdentifier
forIndexPath: indexPath];
if ([kind isEqualToString: UICollectionElementKindSectionHeader]){
Header *header = (Header *)view;
header.label.text = [NSString stringWithFormat:@"Section Header %lu",
(unsigned long)indexPath.section + 1];
}
else if ([kind isEqualToString: UICollectionElementKindSectionFooter]){
Footer *footer = (Footer *)view;
NSString *title = [NSString stringWithFormat:@"Section Footer %lu",
(unsigned long)indexPath.section + 1];
[footer.button setTitle: title forState: UIControlStateNormal];
}
return view;
}
Наконец, необходимо убедиться, что макету с последовательной компоновкой известно о том, что в сборном виде есть ячейки верхнего и нижнего колонтитулов. На основе кода, написанного в разделе 5.3, изменим метод flowLayout делегата нашего приложения следующим образом:
— (UICollectionViewFlowLayout *) flowLayout{
UICollectionViewFlowLayout *flowLayout =
[[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumLineSpacing = 20.0f;
flowLayout.minimumInteritemSpacing = 10.0f;
flowLayout.itemSize = CGSizeMake(80.0f, 120.0f);
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.sectionInset = UIEdgeInsetsMake(10.0f, 20.0f, 10.0f, 20.0f);
/* Задаем базовый размер для видов с верхними и нижними колонтитулами */
flowLayout.headerReferenceSize = CGSizeMake(300.0f, 50.0f);