我在 UICollectionReusableView 方面遇到了一个非常奇怪的问题。我的页脚显示正确,但问题出在我的页眉上。
我的标题是 UICollectionReusableView 的子类并包含:
- 左侧的 UILabel
- 右侧的 UILabel
两个标签都以相同的方式初始化,并且在正确调用的 initWithFrame 方法中具有非常相似的属性。
以下是示例代码部分:
- (id)initWithFrame:(CGRect)frame
{
if((self = [super initWithFrame:frame]))
{
self.backgroundColor = [UIColor whiteColor];
titleLabel = [[UILabel alloc] initWithFrame:CGRectInset(frame, 10, 0)];
titleLabel.font = [UIFont boldSystemFontOfSize:24];
titleLabel.textColor = [UIColor blackColor];
titleLabel.backgroundColor = [UIColor clearColor];
[self addSubview:titleLabel];
dotCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * frame.size.width / 3 - 5, 8, frame.size.width / 3, frame.size.height)];
dotCountLabel.textAlignment = NSTextAlignmentRight;
dotCountLabel.textColor = [UIColor blackColor];
dotCountLabel.backgroundColor = [UIColor clearColor];
[self addSubview:dotCountLabel];
}
return self;
}
第二个标签显示没有任何问题,但奇数索引部分中的第一个标签显示在下一部分标签的顶部。对于任何想要了解我如何创建这些 View 的 View :
SaveHeader *header = (SaveHeader *)[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"saveHeader" forIndexPath:indexPath];
Pattern *pattern = [saveModel.savedPatterns objectAtIndex:indexPath.section];
header.titleLabel.text = pattern.title;
header.dotCountLabel.text = [NSString stringWithFormat:@"%i dots", pattern.dotCount];
return header;
我以前曾多次使用过这些东西,但从未遇到过这样的事情。任何人都知道为什么会发生这种情况以及如何解决?另请注意,我已经通过它运行了一个调试器,数据对象正在返回正确的数据。我使用的是 iOS 7.1。
最佳答案
在调用 initWithFrame 之前,将您将要初始化的 CGRect 存储到一个变量中并检查它。仔细查看值。你可能会在那里找到你的错误。祝你好运!
关于ios - UICollectionReusableView 中的标签仅在偶数索引中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22439866/