解决方法
我认为您可以使用标题的标准行为来获得所需的内容.
设置(可能在viewDidLoad中):
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.bounds.size.width,30); // other setup [self.collectionView setCollectionViewLayout:flowLayout];@H_404_14@然后回答标题视图:
#define MY_HEADER_LABEL_TAG 128 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeader" forIndexPath:indexPath]; UILabel *label = (UILabel *)[headerView viewWithTag:MY_HEADER_LABEL_TAG]; if (!label) { label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds,5,5)]; label.tag = MY_HEADER_LABEL_TAG; label.font = [UIFont boldSystemFontOfSize:12]; label.textColor = [UIColor darkGrayColor]; [headerView addSubview:label]; } label.text = [NSString stringWithFormat:@"Section %d",indexPath.section]; return headerView; }@H_404_14@