2012-11-24 38 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

謝謝,這解決了我的問題! –