我有一个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
使用标签和图像视图作为子视图进行构建。最简单的方法是在情节提要中执行此操作,并在集合视图上显示原型单元。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句