【问题标题】:uicollectionview with custom layout and multiple headers具有自定义布局和多个标题的 uicollectionview
【发布时间】:2014-12-03 08:58:48
【问题描述】:

我有一个带有自定义 UICollectionViewLayout 的 UICollectionView,并希望为每个部分添加多个标题以显示一天中的不同时间。类似于周视图的东西,在我的情况下,时间在左侧垂直,但在顶部和水平方向。

单元格显示良好,但内容很奇怪,看起来单元格被重复使用,屏幕上的 5 个单元格中只有 1 个有内容。其他 4 个标题只有背景颜色,没有内容。

当我滚动时,会出现一些故障,一次只更新一个单元格(有时是 2 个),并且内容可以切换或错位一行。

这是我在控制器中使用的代码 viewDidLoad

[self.epgCollectionView registerClass:[HeaderTimeReusableView class] forSupplementaryViewOfKind:EpgKindTimeRowHeader withReuseIdentifier:HeaderTimeReuseIdentifier];

下面还有相同的控制器:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

UICollectionReusableView *view;
if (kind == EpgKindTimeRowHeader){
   HeaderTimeReusableView *timeRowHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HeaderTimeReuseIdentifier forIndexPath:indexPath];
    timeRowHeader.title.text = [NSString stringWithFormat:@"s%lir%li", (long)indexPath.section, (long)indexPath.row];
    view = timeRowHeader;

}
return view;

}

我想我的自定义 UICollectionViewLayout 很好,因为我可以在我想要的地方看到多个标题。

我的问题是:

  1. 每个部分可以有多个标题吗?
  2. 如何避免这些标头的重用/回收效应?
  3. UICollectionViewCells 也使用 dequeue,但我们可以每个部分有多个单元格,那么为什么它不适用于 headers/UICollectionReusableView 呢?

【问题讨论】:

    标签: uicollectionview uicollectionviewcell uicollectionviewlayout uicollectionreusableview


    【解决方案1】:

    和往常一样,当我发布问题时,我才能找到答案......

    这是我的可重用视图的好代码:

    - (id)initWithFrame:(CGRect)frame
    {
    NSLog(@"HeaderTimeReusableView.h initWithFrame");
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor greenColor];
    
        self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60*3, 30)];
        self.title.backgroundColor = [UIColor redColor];
        self.title.font = [UIFont systemFontOfSize:12.0];
        self.title.text = @"3";
        [self addSubview:self.title];
    
    }
    return self;
    

    }

    我以前曾像这样分配标签框架:

    self.title.frame = frame;
    

    由于某种原因,帧值不是我所期望的。使用硬编码的,它工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      相关资源
      最近更新 更多