使用自定义流布局向集合视图中的标题添加搜索栏

时间:2014-03-02 00:49:13

标签: ios collectionview flowlayout

我正在尝试将搜索栏添加为自定义流布局类的标题。

我知道如果流程布局不是自定义的,只需检查部分标题框,我知道如何在故事板中添加它,但只要我使用自定义类,该选项就不再可用。

大多数情况下,我无法概念化布局属性以及如何实现它们。 此外,我不确定是否应该将方法放入数据源视图控制器或流布局子类,所以请具体的那些。

Ray Wenderlich教程仅显示故事板方法。

1 个答案:

答案 0 :(得分:0)

使用UICollectionReusableView创建HeadeView并调用以下代理。

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        if (kind == UICollectionElementKindSectionHeader) {
            HeaderView *headerView = nil;
        headerView = (ProductListingHeaderView*)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        if (headerView == nil) {
            headerView=[[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil] objectAtIndex:0];
        }
    return headerView;

        }
        return nil;
    }

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
        if (![collectionView numberOfItemsInSection:section]) {
            return CGSizeZero;
        }
        return CGSizeMake([(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout headerReferenceSize].width, searchBarHeight);

    }