Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionViewCell的自定义实现使单元格不可见_Ios_Objective C_Uicollectionview_Uicollectionviewcell_Uicollectionviewlayout - Fatal编程技术网

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

Ios UICollectionViewCell的自定义实现使单元格不可见,ios,objective-c,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Objective C,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,出于上下文的考虑,我正在开发一个具有3种不同视图的日历应用程序。日、月和年视图 为了显示天数,我决定将UICollectionView与UICollectionViewCell的自定义实现一起使用(以便我能够自定义每个单元格的外观) 在myNSObjectCtrlCalendar class I的init上注册3个单元格类,如下所示: //Used for the yearView [self.grid registerClass:[CollectionViewCell class] forCe

出于上下文的考虑,我正在开发一个具有3种不同视图的日历应用程序。日、月和年视图

为了显示天数,我决定将
UICollectionView
UICollectionViewCell
的自定义实现一起使用(以便我能够自定义每个单元格的外观)

在my
NSObject
CtrlCalendar class I的
init
上注册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"];
for (int d = 1; d < self.month.numberOfDays; d++) {
    UILabel *day = [[[UILabel alloc] initWithFrame:dayRect] autorelease];
    day.text = currentDay;
    [cell addSubview:day];
}
然后,在
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
cellTitle
属性设置正确。问题在于这样一个事实,即在出列之后,标签由于某种原因被隐藏

我知道这是一个很长的问题,但如果你能在任何方面提供帮助,或者知道谁可以帮助,请让我知道

非常感谢

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

滚动前:

滚动后:

更新1

正如@Alessandro Chiarotto回答的那样,

[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]
使单元格变空。这里需要注意的是,我需要使用该选择器从单元格中删除我要添加到
yearView
的一些UILabel

My
yearView
UICollection有12个单元格(每个月一个)。每个单元格的UILabel数为每个月的天数。我正在从
collectionView:collectionView单元forItemAtIndexPath:indexPath添加此UILabels,如下所示:

//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"];
for (int d = 1; d < self.month.numberOfDays; d++) {
    UILabel *day = [[[UILabel alloc] initWithFrame:dayRect] autorelease];
    day.text = currentDay;
    [cell addSubview:day];
}
for(int d=1;d

如果我没有在
prepareforeuse
中执行选择器,在滚动视图后,我在每个月单元格中都有所有的
day
标签,一式两份。

如果滚动集合,则将调用
prepareforeuse
。在
prepareforeuse
中,您有以下调用:

[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
这将删除单元格的所有子视图(UILabel等)


此时,您的手机将显示“白色”…

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

如果不是因为问题的另一个方面的话,那就是这样了

正如我在问题更新中所说的,我需要该选择器来删除正在
yearView
中添加的一些UILabel。这些标签是每个月单元格中的天数

解决方案:

我在
CollectionViewCell
中添加了一个
UIView
属性,该属性的帧等于单元格的边界。然后,我将UILabel添加到此视图,并在
prepareforeuse
上执行
self.labelView.subview
上的
removeFromSuperView
。它就像一个符咒

prepareforeuse
的正确实施方法是:

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

谢谢,我刚刚意识到这解决了这个问题,但提出了另一个问题。我正在更新问题!