可滚动日历月视图:集合视图中自定义按钮的标题文本被覆盖

时间:2014-11-02 14:39:28

标签: objective-c calendar uicollectionview

我想创建一个自定义的,可滚动的月视图,用户可以在该视图中滚动相应年份的所有月份(就像iPad日历一样)。

所以我为我需要的所有日期组件创建了一个数组。这个数组中有12个子数组,每个月一个。我已经设法用相应年份的正确日期组件填充我的子数组。

enter image description here

我将collectionView中的部分数量设置为12(每月一个),并将我部分中的项目设置为与当前月份的天数相同。

为了使用日期组件在集合视图单元格中标记我的按钮标题,我编写了此方法来获取标题标签的字符串:

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

标签基本上有效,但是一旦我开始滚动,titleLabel就会被覆盖。 我该如何解决这个问题?在此先感谢您的帮助。

labeling failure

1 个答案:

答案 0 :(得分:0)

解决了它。解决方案非常简单:使用界面构建器我只需在集合视图单元格中拖动一个按钮并将其分配给我的自定义按钮类。此外,我将创建的按钮作为插座连接到我的自定义集合视图单元类。

之后我改变了这样的代码:

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