如何将标题添加到 Collectionview 中的每一列
Posted
技术标签:
【中文标题】如何将标题添加到 Collectionview 中的每一列【英文标题】:How to add header to each column in Collectionview 【发布时间】:2013-11-17 18:53:04 【问题描述】:我创建了一个集合视图,它在每一列中都有不同的数据。但要分开它们,我需要添加 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的个数一样..
【问题讨论】:
【参考方案1】:我已经解决了我的问题。谢谢。
RePrecioHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView" forIndexPath:indexPath];
UILabel *lblHeader=(UILabel *)[headerView viewWithTag:MY_HEADER_LABEL_TAG];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(5,0, 80, 30)];
view.backgroundColor = [UIColor grayColor];
lblHeader = [[UILabel alloc] initWithFrame:CGRectMake(2,3, 60, 10)];
lblHeader.backgroundColor = [UIColor clearColor];
lblHeader.textColor=[UIColor whiteColor];
lblHeader.font = [UIFont systemFontOfSize:11.0f];
lblHeader.numberOfLines=2;
lblHeader.textAlignment=NSTextAlignmentCenter;
lblHeader.text =[[[self.ClassesArr objectAtIndex:indexPath.section]objectAtIndex:0]objectForKey:@"textoClase"];
[view addSubview:lblHeader];
[headerView addSubview:view];
【讨论】:
以上是关于如何将标题添加到 Collectionview 中的每一列的主要内容,如果未能解决你的问题,请参考以下文章
如何将collectionview作为子视图添加到viewcontroller
将视图添加到 StoryBoard 中的 CollectionView 标题
如何使用情节提要将 CollectionView 添加到 ViewController?
如何将手势识别器添加到collectionview单元格中的uiview