ios - UICollectionReusableView 中的标签仅在偶数索引中显示

标签 ios iphone objective-c ios7

我在 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/

相关文章:

ios - 如何将 iOS ui 控件分组到面板中?

iOS firebase数据库查询特定索引中的数据

iphone - nsfetchedresultscontroller 中部分名称键路径的无关系值

iphone - 在 iPhone 上使用 HTTP 摘要式身份验证

ios - NSManagedObject 的单元测试 [GHUnit]

ios - iphone 4s 的媒体查询被用于 Iphone 6

iOS不同类型的对象初始化

ios - 从NSDictionary填充NSArray

ios - .pch 文件中的#define 不起作用

ios - "unrecognized selector sent to instance"用推文创建 NSDictionary 时