ios - 将 header 添加到 UICollectionView,而不是 UICollectionView 的部分

标签 ios storyboard uicollectionview

UITableView 也有类似的功能。我可以向 UITableViewUITableView 中的每个部分添加 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/

相关文章:

Ios - 是否有适合包含大量文本和段落的屏幕的模式或架构?

iphone - Storyboard中的 Facebook SDK?

ios - 放大 UICollectionView 中单个单元格的内容

ios - 突然 EXC_BAD_ACCESS 在 - (NSString *)tableView : titleForHeaderInSection:

iphone - 单元测试很棒,但是

ios - NSLayoutConstraint outlets 始终为 nil 而其他 outlets 则不是

ios - 使用 UICollectionView 传递数据

facebook - 为什么 Facebook 在新闻源中使用 UICollectionView 而不是 UITableView

ios - 如何在 mogenerator 中使用类型安全的结构类型核心数据属性?

ios - 创建 session UUID Swift