1

我有一个带有自定义 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 也使用出队,但我们可以每个部分有多个单元格,那么为什么它不适用于 headers/UICollectionReusableView 呢?
4

1 回答 1

0

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

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

- (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;

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

于 2014-12-03T10:36:42.633 回答