[英]Adding label/button to UICollectionView - Footer or not?
我有一个collectionView以单个水平布局显示图像数组。 我试图在每个单元格下添加一个UILabel。 我认为最好/最简单的方法是为每个单元格添加页脚视图/补充视图。 因此,我覆盖了viewForSupplementaryElementOfKind。 我有一个与图像数组相关的图像名称数组。 所以我只是希望将图像名称作为每个单元格的标签。 但是,所有标签和名称在第一个单元格上都重叠。 另外,直到我滚动到最后一个图像/单元格,才会显示页脚视图,因此,直到滚动到末尾,我才能在第一个单元格上看到重叠的标签。 我知道问题出在我的viewForSupplementaryElementOfKind实现中,但是我不确定如何纠正它。 有人可以请教吗? 提前致谢!!
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
NSLog(@"viewForSupplementaryElementOfKind");
static NSString *FooterCellIdentifier = @"FooterView"; // string value identifier for cell reuse
FooterViewCell *footerView;
if (kind == UICollectionElementKindSectionFooter) {
NSLog(@"*******Element Kind is a footer!*******");
footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:FooterCellIdentifier forIndexPath:indexPath];
for (int i = 0; i < [self.imagesArray count]; i++) {
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.myCollectionView.frame.origin.x/2, self.myCollectionView.frame.origin.y/2, CellWidth, 20)];
//testLabel.text = [self.imageNames objectAtIndex:indexPath.row];
testLabel.text = self.imageNames[i];
[self.myCollectionView addSubview:testLabel];
}
return footerView;
} else {
NSLog(@"*******Element Kind is NOT a footer!*******");
return nil;
}
}
UICollectionView
上的页眉和页脚视图仅相对于部分。
由于标签应该标识图像(模型),因此您应该使用标签和图像视图作为子视图构建UICollectionViewCell
。 最简单的方法是在情节提要中执行此操作,并在集合视图上放置原型单元。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.