UICollectionViewの個々のセルにラベルを追加する際に問題が発生しました。以下に示すコードを使用すると、ラベルはコレクションの最初のセルにのみ追加され、他のセルには追加されません。ただし、最後から3行目のcell.contentViewをcollectionViewに変更すると、ラベルはすべて適切な場所に追加されます。つまり、少なくとも適切なフレームを処理しているのですが、ラベルを追加する必要があります。細胞自体。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CalendarItem"
forIndexPath:indexPath];
UILabel* dayLabel = [[UILabel alloc] initWithFrame:cell.frame];
[dayLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
[dayLabel setBackgroundColor:[UIColor clearColor]];
[dayLabel setTextAlignment:NSTextAlignmentCenter];
[dayLabel setText:@"!"];
[cell.contentView addSubview:dayLabel];
[cell setBackgroundColor:[UIColor whiteColor]];
return cell;
}
私はこれをすべてプログラムで行っているので、使用しているコレクションの初期化コードは次のとおりです。
frame.origin.y = self.view.frame.origin.y + ROW_SIZE;
frame.size.height = self.view.frame.size.height/2 - 2 * ROW_SIZE;
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
CGSize size = CGSizeMake(frame.size.width/7 - 2 * CALENDAR_EDGE_SPACING,
frame.size.height/5 - 2 * CALENDAR_EDGE_SPACING);
[flow setItemSize:size];
[flow setScrollDirection:UICollectionViewScrollDirectionVertical];
calendarView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flow];
[calendarView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CalendarItem"];