怎样在UICollectionView中添加Header和footer

是这样的,想请教一下,怎样在UICollectionView中添加Header和footer
最新回答
许多

2024-09-26 09:18:54

- (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];

}