美文网首页
2023-01-12ClassScheduleDateView

2023-01-12ClassScheduleDateView

作者: 管饱饱 | 来源:发表于2023-01-11 23:52 被阅读0次

    日历选择的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

    相关文章

      网友评论

          本文标题:2023-01-12ClassScheduleDateView

          本文链接:https://www.haomeiwen.com/subject/dfvicdtx.html