我创建了一个在每一列中都有不同数据的集合视图。但要分开它们,我需要添加 header 。我只能添加一个标题。我的代码是:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
UILabel *lblHeader;
if (kind == UICollectionElementKindSectionHeader) {
RePrecioHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView" forIndexPath:indexPath];
NSArray *cellsInSection = self.ClassesArr[indexPath.section];
float collectionWidth = self.view.bounds.size.width;
int cellsInSectionCount = cellsInSection.count;
float cellWidth = (collectionWidth) / cellsInSectionCount;
cellWidth -= 10* (cellsInSectionCount -1);
for(int i=0;i<self.ClassesArr.count; i++){
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(85/self.ClassesArr.count,-5, cellWidth, 40)];
view.backgroundColor = [UIColor grayColor];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 14)];
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont systemFontOfSize:12.0f];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = [[[self.ClassesArr objectAtIndex:i]objectAtIndex:0]objectForKey:@"textoClase"];
[view addSubview:lbl];
[self.collectionView addSubview:view];
}
reusableview = headerView;
}
return reusableview;
}
列号应该类似于 ClassesArr.. 的数量。