gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:35:08 26 4
gpt4 key购买 nike

为了上下文,我正在开发一个具有 3 种不同 View 的日历应用程序。日、月和年 View 。

为了显示日期,我决定使用 UICollectionViewUICollectionViewCell 的自定义实现(以便我能够自定义每个方面细胞)。

在我的 NSObject CtrlCalendar 类的 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"];

然后,在 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

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

经过一些调试后,我发现 CollectionViewCellcellTitle 属性设置正确。问题在于出队后标签由于某种原因被隐藏了。

我知道这是一个很长的问题,但如果您无论如何都可以提供帮助或认识可以提供帮助的人,请告诉我!

非常感谢!

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

滚动前: **Before scrolling**

滚动后: **After scrolling**

更新 1

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

我的 yearView UICollection 有 12 个单元格(每个月一个)。每个单元格都有 UILabels 的数量作为每个月的天数。我从 collectionView:collectionView cellForItemAtIndexPath:indexPath 添加这个 UILabels,如下所示:

for (int d = 1; d < self.month.numberOfDays; d++) {
UILabel *day = [[[UILabel alloc] initWithFrame:dayRect] autorelease];
day.text = currentDay;
[cell addSubview:day];
}

如果我不在 prepareForReuse 中执行选择器,在 ScrollView 后,每个月单元格中的所有 day 标签都是重复的。

最佳答案

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

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

这将删除单元格的所有 subview (UILabel 等)。

此时你的手机会“变白”......

关于ios - UICollectionViewCell 的自定义实现使单元格不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com