UITableView
也有类似的功能。我可以向 UITableView
或 UITableView
中的每个部分添加 header 。这里的问题是如何以编程方式将 header 添加到 UICollectionView
?不是 UICollectionView
的每个部分。
最佳答案
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
if (reusableview==nil) {
reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
label.text=[NSString stringWithFormat:@"Recipe Group #%li", indexPath.section + 1];
[reusableview addSubview:label];
return reusableview;
}
return nil;
}
关于ios - 将 header 添加到 UICollectionView,而不是 UICollectionView 的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27779405/