iOS6 - UICollectionViewFlowLayout未正确放置单元格

时间:2012-11-12 15:53:32

标签: iphone ios uicollectionview

我添加了一个框架大小为320x600的UICollectionView,每个项目大小为44x44。最终输出未正确定位每行中的单元格。有些单元格位于集合视图的边界之外,它看起来像此链接中的图像:http://cl.ly/image/2s270g1p1g2e

#4,#5,#6,#11,#12,#13等在集合视图的范围之外。这是我的UICollectionView和流程布局的代码:

SSCalendarFlowLaout *flowlayout = [[SSCalendarFlowLaout alloc] init];
flowlayout.minimumInteritemSpacing = 1.0f;
flowlayout.minimumLineSpacing = 1.0f;
flowlayout.itemSize = CGSizeMake(44, 44);
flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;

CGRect rect = CGRectMake(0, 0, 320, 600);
self.calendarGrids = [[UICollectionView alloc] initWithFrame:rect
                                          collectionViewLayout:flowlayout];
self.calendarGrids.delegate = self;
self.calendarGrids.dataSource = self;

self.calendarGrids.allowsSelection = YES;
self.calendarGrids.backgroundColor = [UIColor yellowColor];

[self.calendarGrids registerClass:[SSCalendarDayCell class]
         forCellWithReuseIdentifier:@"CalendarDayCell"];

[self addSubview:self.calendarGrids];

SSCalendarFlowLayout是UICollectionViewFlowLayout的子类。将其子类化的原因是调试此问题:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {

  NSArray *unfilteredPoses = [super layoutAttributesForElementsInRect:rect];
  for (UICollectionViewLayoutAttributes *pose in unfilteredPoses) {
    NSLog(@"%@", NSStringFromCGRect(pose.frame));
  }
  return unfilteredPoses;
}

有趣的是,每个项目的layoutAttributesForElementsInRect的输出是正确的,如下面的控制台输出:

2012-11-12 23:42:59.275 Calendar[49367:c07] {{0, 22}, {44, 44}}
2012-11-12 23:42:59.275 Calendar[49367:c07] {{55.2, 22}, {44, 44}}
2012-11-12 23:42:59.276 Calendar[49367:c07] {{110.4, 22}, {44, 44}}
2012-11-12 23:42:59.278 Calendar[49367:c07] {{165.6, 22}, {44, 44}}
2012-11-12 23:42:59.278 Calendar[49367:c07] {{220.8, 22}, {44, 44}}
2012-11-12 23:42:59.279 Calendar[49367:c07] {{276, 22}, {44, 44}}
2012-11-12 23:42:59.279 Calendar[49367:c07] {{0, 76}, {44, 44}}

但为什么输出与UICollectionViewFlowLayout的帧集有很大不同?模拟器和iPhone中的输出都是错误的。

我怀疑这是与此相关的错误:http://openradar.appspot.com/12433891 但与此错误不同的是,UICollectionViewFlowLayout错误地放置了每一行中的单元格,而不仅仅是第一行。

1 个答案:

答案 0 :(得分:0)

问题解决了。我弄错了单元格内的标签的矩形。我在每个单元格中添加了UILabel的子视图,但是在这个问题中发生的是我配置如下标签:

- (id) initWithFrame:(CGRect)frame {

  self = [super initWithFrame:frame];
  if (self) {

    self.dayLabel = [[UILabel alloc] initWithFrame:frame];
    [self.contentView addSubview:self.dayLabel];

  }
  return self;

}

这绝对是错误的。我将框架集更正为:self.dayLabel = [[UILabel alloc] initWithFrame:(CGRect){CGPointZero,frame.size}]; 一切正常。 不管怎样,谢谢