Подтвердить что ты не робот

Добавьте простой UIView в качестве заголовка UICollectionView

У меня есть UICollectionView. Я хотел бы добавить заголовок. Мой заголовок будет только UILabel. Я:

1) выбрал "Заголовок раздела" в качестве аксессуара Collection View в IB.

2) создал коллекцию многоразового представления в IB, со стороны, объявленной как collectionViewHeader.

3) добавили эти строки:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {

            return collectionViewHeader;
    }
    return nil;
}

Но они никогда не называются.

Нужно ли создавать класс только для этой метки, чтобы использовать

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

?

Почему это не так просто, как UITableView, где вы просто перетаскиваете любой заголовок, который хотите?! Все это настолько сложно с помощью UICollectionView...

4b9b3361

Ответ 1

Быстро, как показано ниже

Регистрация заголовка

collectionView.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView")

В UICollectionViewDataSource

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView", forIndexPath: indexPath)

    headerView.frame.size.height = 100

    return headerView
}

Важно, чтобы вы предоставили макет потока с размером заголовка

flowLayout.headerReferenceSize = CGSize(width: self.collectionView.frame.size.width, height: 100)

В противном случае метод источника данных не будет вызван

Ответ 2

Если вы не установите вид заголовка в раскадровке, вам нужно будет зарегистрировать перо.

Пример в viewDidLoad:

- (void)viewDidLoad
{
    [self.collectionView registerClass:NSStringFromClass([YourOwnSubClass class]) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewIdentifier"];
}

В любом случае, вы также можете создать подкласс UICollectionReusableView.

@interface YourOwnSubClass : UICollectionReusableView

затем вызовите делегат в вашем примере класса:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{

    YourOwnSubClass *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                         UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];    
    [self updateSectionHeader:headerView forIndexPath:indexPath];

    return headerView;
}

- (void)updateSectionHeader:(UICollectionReusableView *)header forIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = [NSString stringWithFormat:@"header #%i", indexPath.row];
    header.label.text = text;
}

И не забудьте установить размер заголовка в виде коллекции или в макете потока, чтобы вид заголовка был виден.

Ответ 3

Просто добавьте представление в UICollectionView и измените insets

collectionView.addSubview(headerView)
collectionView.contentInset.top = 300

headerView.frame = CGRect(x: 0,
                            y: -300,
                            width: collectionView.frame.size.width,
                            height: 300)