2023-01-12ClassScheduleDateView

日历选择的view

#import

@protocol ClassScheduleDateViewDelegate <NSObject>

- (void)courseList:(NSString*)startTimeandEndTime:(NSString*)endTime ;

- (void)courseListScrollTo:(NSString *)date;

@end

@interface ClassScheduleDateView : UIView

- (void)setData:(NSArray*)array;

@property (nonatomic, weak) id<ClassScheduleDateViewDelegate>delegate;

@end



#import "ClassScheduleDateView.h"

#import "ClassScheduleCollectionCell.h"

#import "SchoolApiClient.h"

#define titleHeight45

#define weekHeight30

@interface ClassScheduleDateView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (strong, nonatomic) UICollectionView *collectionView;

@property (strong, nonatomic) NSMutableArray *lastlastMonthObjects;

@property (strong, nonatomic) NSMutableArray *lastMonthObjects;

@property (strong, nonatomic) NSMutableArray *currentMonthObjects;

@property (strong, nonatomic) NSMutableArray *nextMonthObjects;

@property (strong, nonatomic) NSMutableArray *nextnextMonthObjects;

@property (strong, nonatomic) NSMutableArray *showDateObjects;

@property (strong, nonatomic) NSMutableArray *showLastDateObjects;

@property (strong, nonatomic) NSMutableArray *showNextDateObjects;

@property (strong, nonatomic) NSArray *weakObjects;

@property (strong, nonatomic) UIView *headView;

@property (strong, nonatomic) UILabel *dateLabel;

@property (strong, nonatomic) UIButton *lastButton;//上个月

@property (strong, nonatomic) UIButton *nextButton;//下个月

@property (strong, nonatomic) UIButton *hideButton;//隐藏日历

@property (strong, nonatomic) NSString *currentDateStr;

@property (strong, nonatomic) NSString *lastDateStr;

@property (strong, nonatomic) NSString *nextDateStr;

@property (strong, nonatomic) NSString *startDateStr;//开始日期

@property (strong, nonatomic) NSString *endDateStr;//结束日期

@property (strong, nonatomic) UIScrollView *bgScrollView;

@property (strong, nonatomic) UICollectionView *lastCollectionView;

@property (strong, nonatomic) UICollectionView *nextCollectionView;

@property (strong, nonatomic) NSMutableArray *courseList;

//请求数据的有课的日子

@property (strong, nonatomic) NSMutableArray *lastList;

@property (strong, nonatomic) NSMutableArray *currentList;

@property (strong, nonatomic) NSMutableArray *nextList;

@property (assign, nonatomic) NSInteger selectIndex;

@end

@implementation ClassScheduleDateView

- (instancetype)initWithFrame:(CGRect)frame {

    self= [superinitWithFrame:frame];

    if(self) {

        self.backgroundColor = [UIColor whiteColor];

        self.lastMonthObjects = [[NSMutableArray alloc]init];

        self.currentMonthObjects = [[NSMutableArray alloc]init];

        self.nextMonthObjects = [[NSMutableArray alloc]init];

        self.showDateObjects = [[NSMutableArray alloc]init];

        self.lastlastMonthObjects = [[NSMutableArray alloc]init];

        self.nextnextMonthObjects = [[NSMutableArray alloc]init];

        self.showLastDateObjects = [[NSMutableArray alloc]init];

        self.showNextDateObjects = [[NSMutableArray alloc]init];

        self.lastList = [[NSMutableArray alloc]init];

        self.currentList = [[NSMutableArray alloc]init];

        self.nextList = [[NSMutableArray alloc]init];

        self.courseList = [[NSMutableArray alloc]init];

        self.weakObjects = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];

        self.currentDateStr = @"";

        self.lastDateStr=@"";

        self.nextDateStr=@"";

        self.startDateStr=@"";

        self.endDateStr=@"";

        self.selectIndex=1000;

        [selfinitHeadView];

        self.bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, titleHeight+weekHeight, SCREEN_WIDTH, self.height-titleHeight-weekHeight)];

        self.bgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*3, self.height-titleHeight-weekHeight);

        self.bgScrollView.pagingEnabled=YES;

        self.bgScrollView.delegate=self;

        self.bgScrollView.showsHorizontalScrollIndicator = NO;

        self.bgScrollView.showsVerticalScrollIndicator = NO;

        self.bgScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);

        [selfaddSubview:self.bgScrollView];

        [self.bgScrollView addSubview:self.collectionView];

        [self.bgScrollView addSubview:self.lastCollectionView];

        [self.bgScrollView addSubview:self.nextCollectionView];


        NSDateFormatter *formatter=[[NSDateFormatter alloc] init];

        [formattersetDateFormat:@"yyyy-MM"];

        NSDate*currentDate=[NSDatenew];

        //上个月

        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

        NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];

        NSDate*lastdate = [calendardateByAddingComponents:lastMonthCompstoDate:currentDateoptions:0];

        NSString*lastDateStr = [formatterstringFromDate:lastdate];


        [selfgetAllDaysWithMonth:lastDateStr];

    }

    return self;

}

- (void)initHeadView{

    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, titleHeight + weekHeight)];

    self.headView.backgroundColor = [UIColor whiteColor];

    [selfaddSubview:self.headView];


    self.dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, titleHeight)];

    self.dateLabel.text=@"";

    [self.headView addSubview:self.dateLabel];


    for(inti =0; i

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.width/7.0*i, titleHeight, self.width/7.0, weekHeight)];

        label.text=self.weakObjects[i];

        label.textColor=RGB(200,200,200);

        label.textAlignment = NSTextAlignmentCenter;

        label.font = [UIFont systemFontOfSize:11];

        [self.headViewaddSubview:label];

    }


    self.lastButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-titleHeight*2-10, 0, titleHeight, titleHeight)];

    [self.lastButton setImage:[UIImage imageNamed:@"classScheduleLeft"] forState:UIControlStateNormal];

    [self.lastButton addTarget:self action:@selector(lastClick) forControlEvents:UIControlEventTouchUpInside];

    [self.headView addSubview:self.lastButton];


    self.hideButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-titleHeight*2-10, 0, titleHeight, titleHeight)];

    [self.hideButton setImage:[UIImage imageNamed:@"classScheduleLeft"] forState:UIControlStateNormal];

    [self.hideButton addTarget:self action:@selector(lastClick) forControlEvents:UIControlEventTouchUpInside];

    [self.headView addSubview:self.hideButton];


    self.nextButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-titleHeight-10, 0, titleHeight, titleHeight)];

    [self.nextButton setImage:[UIImage imageNamed:@"classScheduleRight"] forState:UIControlStateNormal];

    [self.nextButton addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];

    [self.headView addSubview:self.nextButton];

}

- (void)lastClick{

    NSLog(@"lastClick");

    self.currentDateStr = self.lastDateStr;

    self.dateLabel.text = [NSString stringWithFormat:@"%@月",[self.currentDateStr stringByReplacingOccurrencesOfString:@"-" withString:@"年"]];

    [self getAllDaysWithMonth:self.currentDateStr];

}

- (void)nextClick{

    NSLog(@"nextClick");

    self.currentDateStr = self.nextDateStr;

    self.dateLabel.text = [NSString stringWithFormat:@"%@月",[self.currentDateStr stringByReplacingOccurrencesOfString:@"-" withString:@"年"]];

    [self getAllDaysWithMonth:self.currentDateStr];

}

//获取当前月的总天数填充数组

- (void)getAllDaysWithMonth:(NSString *)dateStr//2021-12

{

    [self.lastMonthObjects removeAllObjects];

    [self.nextMonthObjects removeAllObjects];

    [self.showDateObjects removeAllObjects];

    [self.currentMonthObjects removeAllObjects];

    [self.showNextDateObjects removeAllObjects];

    [self.showLastDateObjects removeAllObjects];


    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];

    [formattersetDateFormat:@"yyyy-MM"];

    NSDate*currentDate=[formatterdateFromString:dateStr];

    //上上个月

    NSCalendar *llcalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *lllastMonthComps = [[NSDateComponents alloc] init];

    [lllastMonthCompssetMonth:-2];

    NSDate*lllastdate = [llcalendardateByAddingComponents:lllastMonthCompstoDate:currentDateoptions:0];

    NSString*lllastDateStr = [formatterstringFromDate:lllastdate];

    NSUIntegerlllastDayCount = [selfgetInMonthNumberOfDays:lllastDateStr];//一个月的总天数

    [formattersetDateFormat:@"yyyy-MM-dd"];

    for(NSIntegeri =1; i <= lllastDayCount; i++) {

        NSString * sr = [NSString stringWithFormat:@"%ld",i];

        [self.lastlastMonthObjects addObject:sr];

    }


    //上个月

    [formattersetDateFormat:@"yyyy-MM"];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];

    [lastMonthCompssetMonth:-1];

    NSDate*lastdate = [calendardateByAddingComponents:lastMonthCompstoDate:currentDateoptions:0];

    NSString*lastDateStr = [formatterstringFromDate:lastdate];

    NSUIntegerlastDayCount = [selfgetInMonthNumberOfDays:lastDateStr];//一个月的总天数

    [formattersetDateFormat:@"yyyy-MM-dd"];

    for(NSIntegeri =1; i <= lastDayCount; i++) {

        NSString * sr = [NSString stringWithFormat:@"%ld",i];

        [self.lastMonthObjectsaddObject:sr];

    }

    self.lastDateStr= lastDateStr;


    //下个月

    [formattersetDateFormat:@"yyyy-MM"];

    NSCalendar *nextCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *nextMonthComps = [[NSDateComponents alloc] init];

    [nextMonthCompssetMonth:1];

    NSDate*nextDate = [nextCalendardateByAddingComponents:nextMonthCompstoDate:currentDateoptions:0];

    NSString*nextDateStr = [formatterstringFromDate:nextDate];

    NSUIntegernextDayCount = [selfgetInMonthNumberOfDays:nextDateStr];//一个月的总天数

    [formattersetDateFormat:@"yyyy-MM-dd"];

    for(NSIntegeri =1; i <= nextDayCount; i++) {

        NSString * sr = [NSString stringWithFormat:@"%ld",i];

        [self.nextMonthObjectsaddObject:sr];

    }

    self.nextDateStr= nextDateStr;


    //下下个月

    [formattersetDateFormat:@"yyyy-MM"];

    NSCalendar *nnnextCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *nnnextMonthComps = [[NSDateComponents alloc] init];

    [nnnextMonthCompssetMonth:2];

    NSDate*nnnextDate = [nnnextCalendardateByAddingComponents:nnnextMonthCompstoDate:currentDateoptions:0];

    NSString*nnnextDateStr = [formatterstringFromDate:nnnextDate];

    NSUIntegernnnextDayCount = [selfgetInMonthNumberOfDays:nnnextDateStr];//一个月的总天数

    [formattersetDateFormat:@"yyyy-MM-dd"];

    for(NSIntegeri =1; i <= nnnextDayCount; i++) {

        NSString * sr = [NSString stringWithFormat:@"%ld",i];

        [self.nextnextMonthObjects addObject:sr];

    }

    //当前月

    [formattersetDateFormat:@"yyyy-MM"];

    NSUIntegerdayCount = [selfgetInMonthNumberOfDays:dateStr];//一个月的总天数

    NSString* str = [formatterstringFromDate:currentDate];//年月

    [formattersetDateFormat:@"yyyy-MM-dd"];

    for(NSIntegeri =1; i <= dayCount; i++) {

//        NSString * sr = [NSString stringWithFormat:@"%@-%ld",str,i];

//        [self.showDateObjects addObject:sr];

        NSString * sr = [NSString stringWithFormat:@"%ld",i];

        [self.currentMonthObjects addObject:sr];

    }

    self.currentDateStr= dateStr;

    if ([self.dateLabel.text isEqualToString:@""]) {

        self.dateLabel.text = [NSString stringWithFormat:@"%@月",[self.currentDateStr stringByReplacingOccurrencesOfString:@"-" withString:@"年"]];

    }


    //配置下个月显示的数据

    [formattersetDateFormat:@"yyyy-MM-dd"];

    NSDate*nextFirstDay=[formatterdateFromString:[NSStringstringWithFormat:@"%@-01",nextDateStr]];

    NSDate*nextLastDay=[formatterdateFromString:[NSStringstringWithFormat:@"%@-%lu",nextDateStr,(unsignedlong)nextDayCount]];

    NSIntegernfirstDay = [selfdateisweek:nextFirstDay];

    NSIntegernlastDay = [selfdateisweek:nextLastDay];

    for(inti = (int)nfirstDay; i>0; i--) {

        [self.showNextDateObjects addObject:self.currentMonthObjects[self.currentMonthObjects.count - i]];

    }

    for(inti =0; i

        [self.showNextDateObjects addObject:self.nextMonthObjects[i]];

    }

    for(inti =0; i<6-nlastDay; i++) {

        [self.showNextDateObjects addObject:self.nextnextMonthObjects[i]];

    }


    //配置上个月显示的数据

    [formattersetDateFormat:@"yyyy-MM-dd"];

    NSDate*lastFirstDay=[formatterdateFromString:[NSStringstringWithFormat:@"%@-01",lastDateStr]];

    NSDate*lastLastDay=[formatterdateFromString:[NSStringstringWithFormat:@"%@-%lu",lastDateStr,(unsignedlong)lastDayCount]];

    NSIntegerlfirstDay = [selfdateisweek:lastFirstDay];

    NSIntegerllastDay = [selfdateisweek:lastLastDay];

    for(inti = (int)lfirstDay; i>0; i--) {

        [self.showLastDateObjects addObject:self.lastlastMonthObjects[self.lastlastMonthObjects.count - i]];

    }

    for(inti =0; i

        [self.showLastDateObjects addObject:self.lastMonthObjects[i]];

    }

    for(inti =0; i<6-llastDay; i++) {

        [self.showLastDateObjects addObject:self.currentMonthObjects[i]];

    }


    //配置当前月显示的数据

    [formattersetDateFormat:@"yyyy-MM-dd"];

    NSDate*currentFirstDay=[formatterdateFromString:[NSStringstringWithFormat:@"%@-01",dateStr]];

    NSDate*currentLastDay=[formatterdateFromString:[NSStringstringWithFormat:@"%@-%lu",dateStr,(unsignedlong)dayCount]];

    NSIntegerfirstDay = [selfdateisweek:currentFirstDay];

    NSIntegerlastDay = [selfdateisweek:currentLastDay];

    self.startDateStr = @"";

    self.endDateStr = @"";

    for(inti = (int)firstDay; i>0; i--) {

        [self.showDateObjects addObject:self.lastMonthObjects[self.lastMonthObjects.count - i]];

        if(i == firstDay) {

            self.startDateStr= [NSStringstringWithFormat:@"%@-%@",lastDateStr,self.showDateObjects.firstObject];

        }

    }

    for(inti =0; i

        [self.showDateObjects addObject:self.currentMonthObjects[i]];

        if([self.startDateStrisEqualToString:@""]) {

            self.startDateStr= [NSStringstringWithFormat:@"%@-01",dateStr];

        }

        if(i ==self.currentMonthObjects.count-1) {

            self.endDateStr= [NSStringstringWithFormat:@"%@-%lu",dateStr,(unsignedlong)dayCount];

        }

    }

    for(inti =0; i<6-lastDay; i++) {

        [self.showDateObjects addObject:self.nextMonthObjects[i]];

        if(i ==6-lastDay-1) {

            self.endDateStr= [NSStringstringWithFormat:@"%@-%lu",nextDateStr,(unsignedlong)i+1];

        }

    }

    [self.lastCollectionView reloadData];

    [self.nextCollectionView reloadData];

    [self.collectionView reloadData];

    self.bgScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);


    if(self.delegate&& [self.delegaterespondsToSelector:@selector(courseList:andEndTime:)]) {

        [self.delegate courseList:self.startDateStr andEndTime:self.endDateStr];

    }

}

-(NSInteger)getInMonthNumberOfDays:(NSString *)dateStr

{

    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];

    [formattersetDateFormat:@"yyyy-MM"];

    NSDate*currentDate=[formatterdateFromString:dateStr];

    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit: NSCalendarUnitMonth forDate:currentDate];

    returnrange.length;

}

- (void)setupRequestMonth

{

    NSDate*currentDate = [NSDatedate];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formattersetDateFormat:@"yyyy-MM"];


    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];

//    [lastMonthComps setYear:1]; // year = 1表示1年后的时间 year = -1为1年前的日期,month day 类推

    [lastMonthCompssetMonth:-1];

    NSDate*newdate = [calendardateByAddingComponents:lastMonthCompstoDate:currentDateoptions:0];

    NSString*dateStr = [formatterstringFromDate:newdate];

    NSLog(@"date str = %@", dateStr);

}

- (NSInteger)dateisweek:(NSDate*)date {

    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *comps = [calendar components:kCFCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:date];

    NSIntegernum;

    switch(comps.weekday) {

        case1:

            num =0;//周日

            break;

        case2:

            num =1;

            break;

        case3:

            num =2;

            break;

        case4:

            num =3;

            break;

        case5:

            num =4;

            break;

        case6:

            num =5;

            break;

        case7:

            num =6;//周六

            break;


        default:

            break;

    }

    returnnum;

}

- (UICollectionView *)collectionView{

    if (!_collectionView) {

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

        [flowLayoutsetScrollDirection:UICollectionViewScrollDirectionVertical];

        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, self.height-titleHeight-weekHeight-10) collectionViewLayout:flowLayout];

        _collectionView.delegate = self;

        _collectionView.dataSource = self;

        _collectionView.backgroundColor = [UIColor whiteColor];

        //        添加headview 实际上是 使用了uicollectionview的 SectionHeader来实现类似于  tableview 的 tableheaderview效果

//        [_collectionView registerNib:[UINib nibWithNibName:@"PositionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PositionReusableView"];

        [_collectionView registerNib:[UINib nibWithNibName:@"ClassScheduleCollectionCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([ClassScheduleCollectionCell class])];

        [selfaddSubview:self.collectionView];

    }

    return _collectionView;

}

- (UICollectionView *)lastCollectionView{

    if (!_lastCollectionView) {

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

        [flowLayoutsetScrollDirection:UICollectionViewScrollDirectionVertical];

        _lastCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height-titleHeight-weekHeight-10) collectionViewLayout:flowLayout];

        _lastCollectionView.delegate = self;

        _lastCollectionView.dataSource = self;

        _lastCollectionView.backgroundColor = [UIColor whiteColor];

        //        添加headview 实际上是 使用了uicollectionview的 SectionHeader来实现类似于  tableview 的 tableheaderview效果

//        [_collectionView registerNib:[UINib nibWithNibName:@"PositionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PositionReusableView"];

        [_lastCollectionView registerNib:[UINib nibWithNibName:@"ClassScheduleCollectionCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([ClassScheduleCollectionCell class])];

        [self addSubview:_lastCollectionView];

    }

    return _lastCollectionView;

}

- (UICollectionView *)nextCollectionView{

    if (!_nextCollectionView) {

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

        [flowLayoutsetScrollDirection:UICollectionViewScrollDirectionVertical];

        _nextCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*2, 0, SCREEN_WIDTH, self.height-titleHeight-weekHeight-10) collectionViewLayout:flowLayout];

        _nextCollectionView.delegate = self;

        _nextCollectionView.dataSource = self;

        _nextCollectionView.backgroundColor = [UIColor whiteColor];

        //        添加headview 实际上是 使用了uicollectionview的 SectionHeader来实现类似于  tableview 的 tableheaderview效果

//        [_collectionView registerNib:[UINib nibWithNibName:@"PositionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PositionReusableView"];

        [_nextCollectionView registerNib:[UINib nibWithNibName:@"ClassScheduleCollectionCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([ClassScheduleCollectionCell class])];

        [self addSubview:_nextCollectionView];

    }

    return _nextCollectionView;

}

//#pragma mark - 代理方法 Delegate Methods

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

//    return CGSizeMake(SCREEN_WIDTH, 50);

//}

////然后实现以下的协议

//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

//{

//    UICollectionReusableView *reusableview = nil;

//    NSLog(@"kind = %@", kind);

//    if (kind == UICollectionElementKindSectionHeader){

//

//        PositionReusableView *headerV = (PositionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PositionReusableView" forIndexPath:indexPath];

//        headerV.titleLabel.text = self.objects[indexPath.section][@"cate"][@"name"];

//        reusableview = headerV;

//        NSString *cateCode = self.objects[indexPath.section][@"cate"][@"code"];

//        NSString *logoStr = [self getLogoNameStr:cateCode];

//        headerV.logoImageView.image = [UIImage imageNamed:logoStr];

//        if (indexPath.section == 0) {

//            headerV.lineView.hidden = YES;

//        }

//    }

//    return reusableview;

//}

//- (NSString *)getLogoNameStr:(NSString *)code{

//    if ([code isEqualToString:@"1"]) {

//        return @"position_logo_gwy";

//    }

//    else if ([code isEqualToString:@"2"]) {

//        return @"position_logo_tang";

//    }

//    else if ([code isEqualToString:@"3"]) {

//        return @"position_logo_xz";

//    }

//    else if ([code isEqualToString:@"4"]) {

//        return @"position_logo_jiguan";

//    }

//    return @"position_logo_jiguan";

//}

// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return1;

//    return self.currentMonthObjects.count;

}

// 每个分区上得元素个数

- (NSInteger)collectionView:(UICollectionView*)collectionViewnumberOfItemsInSection:(NSInteger)section {

    if(collectionView ==self.collectionView) {

        return self.showDateObjects.count;

    }

    elseif(collectionView ==self.nextCollectionView){

        return  self.showNextDateObjects.count;

    }

    elseif(collectionView ==self.lastCollectionView){

        return  self.showLastDateObjects.count;

    }

    return0;

}

// 设置cell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    ClassScheduleCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ClassScheduleCollectionCell class]) forIndexPath:indexPath];

    NSString*day =@"";

    if(collectionView ==self.collectionView) {

        day =self.showDateObjects[indexPath.row];

        cell.bgView.backgroundColor = [UIColor whiteColor];

    }

    elseif(collectionView ==self.nextCollectionView){

        day =self.showNextDateObjects[indexPath.row];

    }

    elseif(collectionView ==self.lastCollectionView){

        day =self.showLastDateObjects[indexPath.row];

    }

    cell.dateLabel.text= day;

    if(indexPath.row<10&& day.intValue>20) {

        cell.dateLabel.textColor=RGB(200,200,200);

        cell.dateLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];

        if([self.lastListcontainsObject:day]) {

            cell.radioView.hidden=NO;

        }

        else{

            cell.radioView.hidden=YES;

        }

    }

    elseif(indexPath.row>20&& day.intValue<10) {

        cell.dateLabel.textColor=RGB(200,200,200);

        cell.dateLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];

        if([self.nextListcontainsObject:day]) {

            cell.radioView.hidden=NO;

        }

        else{

            cell.radioView.hidden=YES;

        }

    }

    else{

        cell.dateLabel.textColor=UIColorFromRGB(0x000000);

        cell.dateLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];

        if([self.currentListcontainsObject:day]) {

            cell.radioView.hidden=NO;

        }

        else{

            cell.radioView.hidden=YES;

        }

    }

    if(collectionView ==self.collectionView) {

        if(self.selectIndex== indexPath.row) {

            cell.bgView.backgroundColor=UIColorFromRGB(UI_COLOR_Exam_BLUE_2987FC);

            cell.dateLabel.textColor = [UIColor whiteColor];

        }

    }

    returncell;

}

// 设置cell大小 itemSize:可以给每一个cell指定不同的尺寸

- (CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutsizeForItemAtIndexPath:(NSIndexPath*)indexPath {

    intnum = (int)self.showDateObjects.count/7;

    CGFloatheight = (self.height-titleHeight-weekHeight-10)/num;

    CGFloatwidth =SCREEN_WIDTH/7.0;

    returnCGSizeMake(width, height);

}

// 设置UIcollectionView整体的内边距(这样item不贴边显示)

- (UIEdgeInsets)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section {

    // 上 左 下 右

    return UIEdgeInsetsMake(0, 0, 0, 0);

}

// 设置minimumLineSpacing:cell上下之间最小的距离

- (CGFloat)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutminimumLineSpacingForSectionAtIndex:(NSInteger)section {

    return0;

}

// 设置minimumInteritemSpacing:cell左右之间最小的距离

- (CGFloat)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutminimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

    return0;

}

// 选中cell的回调

- (void)collectionView:(UICollectionView*)collectionViewdidSelectItemAtIndexPath:(NSIndexPath*)indexPath {

    self.selectIndex= indexPath.row;

    [self.collectionView reloadData];

    if(self.delegate&& [self.delegaterespondsToSelector:@selector(courseListScrollTo:)]) {

        NSString*day =@"";

        if(collectionView ==self.collectionView) {

            day =self.showDateObjects[indexPath.row];

        }

        if(day.intValue<10) {

            day = [NSStringstringWithFormat:@"0%@",day];

        }

        if(indexPath.row<10&& day.intValue>20) {

            day = [NSStringstringWithFormat:@"%@-%@",self.lastDateStr,day];

        }

        elseif(indexPath.row>20&& day.intValue<10) {

            day = [NSStringstringWithFormat:@"%@-%@",self.nextDateStr,day];

        }

        else{

            day = [NSStringstringWithFormat:@"%@-%@",self.currentDateStr,day];

        }

        [self.delegate courseListScrollTo:day];

    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    BOOLscrollToScrollStop = !scrollView.tracking&& !scrollView.dragging&& !scrollView.decelerating;

    if(scrollToScrollStop) {

        // 停止后要执行的代码

        NSLog(@"停止后要执行的代码11111");

        CGFloatx = scrollView.contentOffset.x;

        NSLog(@"停止后要执行的代码11111----%f--------",x);

        if(x ==0) {

            //向左滑动

            [selflastClick];

            self.selectIndex=100;

        }

        elseif(x ==SCREEN_WIDTH*2){

            //向右滑动

            [selfnextClick];

            self.selectIndex=100;

        }

    }

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    if(!decelerate) {

        BOOLdragToDragStop = scrollView.tracking&& !scrollView.dragging&& !scrollView.decelerating;

        if(dragToDragStop) {

            // 停止后要执行的代码

            NSLog(@"停止后要执行的代码222222");

        }

    }

}

//刷新下蓝色点点

- (void)setData:(NSArray*)array{

    [self.lastList removeAllObjects];

    [self.currentList removeAllObjects];

    [self.nextList removeAllObjects];

    for(NSDictionary*dicinarray) {

        NSString*time = dic[@"startTime"];

        if(time.length>8) {

            NSString *day = [NSString stringWithFormat:@"%d",[time substringFromIndex:8].intValue];

            time = [timesubstringToIndex:7];

            if([timeisEqualToString:self.currentDateStr]) {

                [self.currentListaddObject:day];

            }

            elseif([timeisEqualToString:self.nextDateStr]){

                [self.nextListaddObject:day];

            }

            elseif([timeisEqualToString:self.lastDateStr]){

                [self.lastListaddObject:day];

            }

        }

    }

    [self.collectionView reloadData];

}

@end

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容