UICollectionViewCell的自定义实现使单元格不可见

时间:2015-12-09 19:50:37

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

为了上下文,我正在开发一个包含3个不同视图的日历应用程序。日,月和年视图。

为了显示我决定使用UICollectionView并使用UICollectionViewCell的自定义实现的日子(以便我能够自定义每个单元格的方面)。

在我的init CtrlCalendar类的NSObject上,我注册了3个单元格类:

//Used for the yearView
[self.grid registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"monthCell"];

//Used for the monthView
[self.grid registerClass:[CollectionViewCell class] forC ellWithReuseIdentifier:@"dayCell"];

//Used for the dayView
[self.grid registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"hourCell"];

collectionView:collectionView cellForItemAtIndexPath:indexPath我做的如下:

CollectionViewCell *cell = nil;
if ([self.currentDisplayType isEqualToString:@"monthView"]) {
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dayCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor grayColor];
    self.month = [self.displayingViews get:indexPath.section];
    if (indexPath.row + 1 >= self.month.firstWeekDay && indexPath.row + 1 < self.month.numberOfDays + (self.month.firstWeekDay)) {
        NSDateComponents *tempDay = [self.calendar components:NSCalendarUnitDay fromDate:self.month.date];
        cell.date = [self.dateHelper addToDate:self.month.date days:(int)(tempDay.day + indexPath.row - self.month.firstWeekDay)];
        cell.cellTitle.text = [NSString stringWithFormat:@"%i", (int)(tempDay.day + indexPath.row + 1 - self.month.firstWeekDay)];
        cell.cellTitle.textColor = [UIColor blackColor];

        if ([[self.dateHelper addToDate:self.month.date days:(indexPath.row + 1 - self.month.firstWeekDay)] isEqualToDate:self.today]) {
            cell.cellTitle.textColor = [UIColor redColor];
        }
        cell.userInteractionEnabled = YES;
        cell.separator.hidden = NO;
        cell.contentView.hidden = NO;

    } else {
        cell.userInteractionEnabled = NO;
    }

最后,这是CollectionViewCell的实现:

@interface CollectionViewCell ()

@property (nonatomic, retain) UILabel *cellTitle;
@property (nonatomic, retain) NSString *titleText;
@property (nonatomic, retain) UILabel *separator;
@property (nonatomic, retain) NSDate *date;

@end

@implementation CollectionViewCell

@synthesize cellTitle;
@synthesize titleText;
@synthesize separator;
@synthesize date;

- (void)dealloc {
    [cellTitle release];
    [titleText release];
    [separator release];
    [date release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.cellTitle = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
        self.cellTitle.textColor = [UIColor blackColor];
        self.cellTitle.textAlignment = NSTextAlignmentCenter;

        self.separator = [[[UILabel alloc] initWithFrame:CGRectMake(0, (self.frame.size.height - 0.25), self.frame.size.width, 0.5)] autorelease];
        self.separator.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:self.separator];
        [self.contentView addSubview:self.cellTitle];
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.cellTitle.text = nil;
    self.separator.hidden = YES;
    [self.cellTitle setTextColor:[UIColor blackColor]];
    //the presence of this line is explained after the edit bellow
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}

@end

我第一次运行应用程序时,月视图按预期配置,但一旦执行出列,单元格就会变为空白。

经过一些调试后,我发现CollectionViewCell的{​​{1}}属性已正确设置。问题在于,在出列之后,标签因某种原因被隐藏。

我知道这是一个很长的问题,但如果您可以提供帮助或知道有人可以提供帮助,请告诉我们!

非常感谢!

为了澄清问题,我正在添加一些屏幕

滚动前: **Before scrolling**

滚动后: **After scrolling**

更新1

@Alessandro Chiarotto回答说,cellTitle 确实让细胞变空了。这里需要注意的是,我需要选择器从单元格中删除一些我正在添加到 [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]的UILabel。

我的yearView UICollection有12个单元格(每月一个)。每个单元格的UILabel数量都是每个月的天数。我正在添加yearView这样的UILabel:

collectionView:collectionView cellForItemAtIndexPath:indexPath

如果我没有在for (int d = 1; d < self.month.numberOfDays; d++) { UILabel *day = [[[UILabel alloc] initWithFrame:dayRect] autorelease]; day.text = currentDay; [cell addSubview:day]; } 中执行选择器,滚动视图后,我会在每个月的单元格中重复所有prepareForReuse个标签。

2 个答案:

答案 0 :(得分:2)

如果滚动集合,则会调用prepareForReuse。在prepareForReuse中,您有以下电话:

[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

这将删除单元格的所有子视图(UILabel等)。

此时你的细胞将“白色”......

答案 1 :(得分:0)

我发现了问题。正如@Alessandro在他的回答中所说,[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];正在删除单元格中的所有内容,因此将其留空。

如果不是问题的另一个方面,那就是它。

正如我在更新问题中所说,我需要选择器才能删除yearView中添加的一些UILabel。这些标签是每个月细胞的日子。

<强>解决方案:

我向UIView添加了一个CollectionViewCell属性,其框架等于单元格的边界。我将UILabel添加到此视图,然后在prepareForReuseremoveFromSuperView执行self.labelsView.subviews。它就像一个魅力。

prepareForReuse的正确实施将是:

- (void)prepareForReuse {
    [super prepareForReuse];
     self.cellTitle.text = nil;
     self.separator.hidden = YES;
    [self.cellTitle setTextColor:[UIColor blackColor]];
    [self.labelsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}