Objective c 可滚动日历月视图:集合视图中自定义按钮的标题文本被覆盖
我想创建一个自定义的、可滚动的月份视图,在该视图中,用户可以像iPad日历一样滚动相应年份的所有月份 所以我为我需要的所有日期组件创建了一个数组。此阵列中有12个子阵列,每个月一个子阵列。我已经设法用相应年份的正确日期组件填充子数组 我将collectionView中的节数设置为每月12节,并且节中的项目数与当月的天数相同 为了用日期组件标记集合视图单元格中的按钮标题,我编写了以下方法来获取标题标签的字符串:Objective c 可滚动日历月视图:集合视图中自定义按钮的标题文本被覆盖,objective-c,calendar,uicollectionview,Objective C,Calendar,Uicollectionview,我想创建一个自定义的、可滚动的月份视图,在该视图中,用户可以像iPad日历一样滚动相应年份的所有月份 所以我为我需要的所有日期组件创建了一个数组。此阵列中有12个子阵列,每个月一个子阵列。我已经设法用相应年份的正确日期组件填充子数组 我将collectionView中的节数设置为每月12节,并且节中的项目数与当月的天数相同 为了用日期组件标记集合视图单元格中的按钮标题,我编写了以下方法来获取标题标签的字符串: - (NSString *)getDayOfMonthFromComponent: (
- (NSString *)getDayOfMonthFromComponent: (NSDateComponents *)components{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *newDate = [gregorian dateByAddingComponents:components toDate:self.currentDate options:0];
NSDateFormatter *dateFormatterDayOfMonth = [[NSDateFormatter alloc]init];
[dateFormatterDayOfMonth setDateFormat:@"d"]; // filter day of month
NSString *dayOfMonth = [dateFormatterDayOfMonth stringFromDate:newDate];
return dayOfMonth;
}
这就是我标记自定义按钮的方式:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
EMMonthCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MonthCell" forIndexPath:indexPath];
EMBorderlessButton *button;
#pragma mark - dateLabel sort algorithm iPhone
if (IS_IPHONE){
#pragma mark - dateLabel sort algorithm iPad
} else if (IS_IPAD){
button = [[EMBorderlessButton alloc]initWithFrame:CGRectMake(110, 10, 30, 30)];
NSString *titleText;
NSLog(@"index path: %ld of section: %ld", (long)indexPath.row, (long)indexPath.section);
if (indexPath.row < [[self.daysThisMonthArray objectAtIndex:indexPath.section] intValue]){
titleText = [self getDayOfMonthFromComponent:self.dateComponents [indexPath.section] [indexPath.row]];
}
[button setTitle:titleText forState:UIControlStateNormal];
[button setUserInteractionEnabled:NO];
[button setSelected:FALSE];
[button setTag:indexPath.row];
[cell addSubview:button];
[self.buttonArray addObject:button];
[cell bringSubviewToFront:button];
}
return cell;
}
标签基本上有效,但一旦我开始滚动,标题标签就会被覆盖。
我如何解决这个问题?提前感谢你的帮助
解决了。解决方案非常简单:使用interface builder,我只需在集合视图单元格中拖动一个按钮,并将其分配给自定义按钮类。此外,我将创建的按钮作为插座连接到我的自定义集合视图单元类 之后,我更改了如下代码:
#pragma mark - dateLabel sort algorithm iPad
} else if (IS_IPAD){
NSLog(@"check content: %@", self.daysThisMonthArray);
EMBorderlessButton *theLabel = (EMBorderlessButton *)[cell viewWithTag:100];
[theLabel setTitleColor:[UIColor em_darkViolett] forState:UIControlStateNormal];
if (indexPath.row < [[self.daysThisMonthArray objectAtIndex:indexPath.section] intValue]){
[cell.dateButton setTitle:[self getDayOfMonthFromComponent:self.dateComponents [indexPath.section] [indexPath.row]] forState:UIControlStateNormal];
if ([[self getDayOfMonthFromComponent:self.dateComponents [indexPath.section] [indexPath.row]] isEqualToString:@"1"]){
[cell.dateButton setTitle:[NSString stringWithFormat:@"%@. %@", [self getDayOfMonthFromComponent:self.dateComponents [indexPath.section] [indexPath.row]], [self getMonthNameFromComponents:self.dateComponents [indexPath.section] [indexPath.row]]] forState:UIControlStateNormal];
}
}
[self.buttonArray addObject:theLabel];
}
return cell;
}