坑爹的UICollectionView

最近用UICoolectionView的时候遇到一个很DT的问题,我往VC里加12个视图,结果显示成这样(右边是期待的样子):

     

研究了一下午,终于发现了问题:

@interface FpLabelCell : UICollectionViewCell

@property (strong, nonatomic) UILabel *label;

@end

@implementation FpLabelCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _label = [[UILabel alloc]initWithFrame:frame];    //这里直接采用Cell的frame
        [self addSubview:_label];
    }
    return self;
}

@end

请注意这个地方:

_label = [[UILabel alloc]initWithFrame:frame];

frame是Cell的frame,这里我们的目的是让label的大小跟cell一样,但是有一个问题,就是cell的frame是带着x、y的,这样会让label的起始位置产生了偏移,从而引入了问题。

改成下面这样就好了:

_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];

花了我一下午啊啊啊!

posted @ 2014-11-10 18:13  alexcai  阅读(241)  评论(0编辑  收藏  举报