UICollectionView 的 cellForItemAtIndexPath 无法识别单个单元格框架 [英] UICollectionView's cellForItemAtIndexPath not recognizing individual cell frames

查看:19
本文介绍了UICollectionView 的 cellForItemAtIndexPath 无法识别单个单元格框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向 UICollectionView 中的单个单元格添加标签时遇到问题.使用下面显示的代码,标签仅添加到集合中的第一个单元格,其他单元格都不添加.但是,如果我将第三行到最后一行中的 cell.contentView 更改为 collectionView,标签都会添加到正确的位置,这意味着我至少要处理正确的框架,但我需要将标签添加到细胞本身.

I am having an issue adding labels to individual cells in a UICollectionView. Using the code shown bellow, the label is added only to the first cell in the collection and none of the others. However, if I change cell.contentView in the third to last line to collectionView, the labels are all added to the right locations, which means I'm dealing with at least the right frame, but I need the labels to be added to the cells themselves.

- (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;
}

我正在以编程方式执行所有这些操作,因此这是我正在使用的集合的初始化代码:

I'm doing this all programmatically so here is my initialization code for the collection I'm using:

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"];

推荐答案

你不应该引用单元格的框架.集合视图会移动它们,移除它们,并且通常会对您不想考虑的单元格做一些意想不到的事情.

You should never be referencing the cell's frame. The collection view moves them around, removes them and generally does unexpected things with cells that you don't want to have to think about.

如果您希望标签占据整个单元格,您需要将标签的框架设置为单元格的 contentView 框架.如果你这样做,一切都应该开始完美运行.

If you want your label to take up the entire cell you want to set the label's frame to the cell's contentView's frame. Everything should start working perfectly if you do that.

使用 collectionView 代替 cell.contentView 的原因是因为实际上,单元格位于集合视图中的某个任意位置,例如 (150,1024).因此,当您在原点 (150, 1024) 处向内容视图添加标签时,它实际上位于您正在查看的视图下方;但是当您将标签直接添加到集合视图时,它与单元格位于同一位置.

The reason using collectionView works in place of cell.contentView is because, in reality, the cell is at some arbitrary position within the collection view, say (150, 1024). So when you add a label to the content view at origin (150, 1024), it's actually way below the view that you're looking at; but when you add the label directly to the collection view it's at the same position as the cell.

这篇关于UICollectionView 的 cellForItemAtIndexPath 无法识别单个单元格框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆