2012-11-24 36 views
3

我,我有细胞的每一行后继承UICollectionViewController.UICollectionView标题重复每一行

UITableView标题重复。 我需要头是“单”,并始终在屏幕的顶部(如导航栏)。

我用下面的代码: 我UICollectionViewController类:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return [categoryArray count]/2; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 2; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryCell" forIndexPath:indexPath]; 
    CategoryViewModel *category = [categoryArray objectAtIndex:(indexPath.section*2 + indexPath.row)]; 
    cell.name.text = category.name; 
    cell.image.image = category.image; 
    return cell; 
} 

-(UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ 
    if(headerIndexPath == nil){ 
     headerIndexPath = indexPath; 
    } 
    CategoryScreenHeader *categoryScreenHeader = [collectionView dequeueReusableSupplementaryViewOfKind: 
             UICollectionElementKindSectionHeader withReuseIdentifier:@"CategoryScreenHeader" forIndexPath:headerIndexPath]; 
    categoryScreenHeader.headerName.text = @"Some title"; 
    categoryScreenHeader.headerImage.image = [UIImage imageNamed:@"step-0.png"]; 
    return categoryScreenHeader; 
} 

谢谢。

+0

你使用什么布局? – chris

+0

我正在使用UICollectionViewLayout –

回答

0

UICollectionView没有像UITableView这样的表头(据我所知),只有与内容一起滚动的节标题。

如果你想在屏幕顶部留下一个视图,那么你应该添加2个子视图到你的控制器的视图中,一个用于你的标题,一个用于集合视图。将集合视图插入到底部子视图中,而不是插入到控制器的主视图中。删除collectionView中的代码:viewForSupplementaryElementOfKind:atIndexPath :(除非你想要节标题)。

+0

谢谢,这解决了我的问题! –