[英]UICollectionView header repeats every row
我是UICollectionViewController.
子類UICollectionViewController.
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;
}
謝謝。
UICollectionView沒有像UITableView那樣的表頭(據我所知),只有與內容一起滾動的節頭。
如果您希望屏幕頂部的視圖停留在那里,那么您應該將2個子視圖添加到控制器的視圖中,一個用於標題,另一個用於集合視圖。 將集合視圖插入到底部子視圖中,而不是插入控制器的主視圖中。 刪除collectionView中的代碼:viewForSupplementaryElementOfKind:atIndexPath :(除非你想要節標題)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.