UICollectionView纯代码,如何去除Section Header

 我来答
xiangjuan314
2016-04-18 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2749万
展开全部
代码设计Section的header和Footer
好多小伙伴都在找UICollectionView是否有这么个属性,比如上图说到Accessories什么的,其实不然。大家首先要搞明白意见事情,header和footer是追加视图,属于layout中的,所以要代码设置section要在UICollectionViewFlowLayout:

- (UICollectionViewFlowLayout *) flowLayout{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.。。。。。。//各属性设置
flowLayout.headerReferenceSize = CGSizeMake(300.0f, 50.0f); //设置head大小
flowLayout.footerReferenceSize = CGSizeMake(300.0f, 50.0f);
return flowLayout;
}

如果你用的是Xib或者Storyboard,不想在上图属性设置,想用代码:

- (void)viewDidLoad
{
[super viewDidLoad];

//这个地方一定要写,不然会crash
[_ui_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"GradientCell"];

[_ui_collectionView registerClass:[RecipeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

//代码控制header和footer的显示
UICollectionViewFlowLayout *collectionViewLayout = (UICollectionViewFlowLayout *)_ui_collectionView.collectionViewLayout;
collectionViewLayout.headerReferenceSize = CGSizeMake(375, 50);

}

其中_ui_collectionView是:

@property (weak, nonatomic) IBOutlet UICollectionView *ui_collectionView;


说明(这部分说明可以参见xib设置sectionview):
当然要让上述代码起作用还要注册UICollectionReusableView的派生类(上述代码中的RecipeCollectionReusableView),还要实现:

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;

if (kind == UICollectionElementKindSectionHeader)
{
RecipeCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

reusableview = headerView;
}

// if (kind == UICollectionElementKindSectionFooter)
// {
// RecipeCollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
//
// reusableview = footerview;
// }

reusableview.backgroundColor = [UIColor redColor];

return reusableview;
}
心太抽痛
2016-04-18 · TA获得超过1431个赞
知道大有可为答主
回答量:2234
采纳率:0%
帮助的人:2691万
展开全部
Split Recipes into Two Sections in UICollectionView
在这个简单的程序中,RecipeCollectionViewController是集合视图的数据源对象,为了把视图分成两个部分,我们需要有一些变化,接下来我们完成:

起先,recipeImages数组是存储所有recipes的名称,因为我们想把recipes分成两组,我们要修改我们的代码,并使用签到数组来存储
不同的recipe,也许你还不明白啥是嵌入的数组,下面的图片会让你明白的。第一组包含主要的图像,而另一个为drink和dessert。顶级数组
(即recipeImages)包含两个数组,每个数组部分的特定区域包含特定的data items。

让我们开始编写代码,在RecipeCollectionViewController.m中初始化"recipeImages"数组,并在viewDidload方法中写下面的方法:
- (void)viewDidLoad
{
[super viewDidLoad];
//Initialize recipe image array

NSArray *mainDishImages = [NSArray
arrryWithObjects:@"egg_benedict.jpg", @"full_breakfast.jpg",
@"ham_and_cheese_panini.jpg", @"ham_and_egg_sandwich.jpg",
@"hamburger.jpg", @"instant_noodle_with_egg.jpg",
@"japanese_noodle_with_pork.jpg", @"mushroom_risotto.jpg",
@"noodle_with_bbq_pork.jpg", @"thai_shrimp_cake.jpg",
@"vegetable_curry.jpg", nil];
NSArray
*drinkDessertImages = [NSArray arrayWithObjects:@"angry_birds_cake.jpg",
@"creme_brelee.jpg", @"green_tea.jpg", @"starbucks_coffee.jpg",
@"white_chocolate_donut.jpg", nil];
recipeImages = [NSArray arrayWithObjects:mainDishImages,drinkDesserImages,nil];
}
上面的代码将recipes images分成两组。接下来,修改"numberOfIntemsInSecion:"方法来返回,每个secions中的items数目:

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSecion:(NSInteger)section
{
return [[recipeImages objectAtIndex:sectin]count];
}
接下来我们按照下面的方法修改"cellForItemAtIndexPath:"方法

- (UICollectionVIewCell
*)collectionView:(UICollectionView*)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";

RecipeViewCell *cell = (RecipeViewCell *)[collectionView
dequeueReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imagedNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame-2.png"]];
return cell;
}
你可以和以前的代码比较以下,你就会知道只有一样是唯一的变化。我们首先检索该数组的section number然后从section中获取具体的items。

最后,怎样给collection
view实现两个section,这个可以通过方法调用下面的方法来完成即:在RecipeCollectionViewController.m中的
numberOfSectionsInCollectionView方法,在collectin View中返回section中的数量。
- (NSInteger)numberOfSectionsInCollectionVIew:(UICollectionView *)collectionView
{
return [recipeImages count];
}
现在运行你的app,你会在屏幕上看到下面的显示
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式