// // GWPCourseListView.m // CourseList // // Created by GanWenPeng on 15/12/3. // Copyright © 2015年 GanWenPeng. All rights reserved. // #import "GWPCourseListView.h" #define MaxDay 7 @protocol CourseSort @property (nonatomic, assign) NSUInteger sortIndex; @end @interface CourseCell : UITableViewCell @property (nonatomic, strong) id course; /** 分割线 */ @property (nonatomic, weak) UIView *sepLine; @end @implementation CourseCell - (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) { [self setup]; } return self; } - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self setup]; } return self; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self setup]; } return self; } - (void)setup{ UIView *sep = [[UIView alloc] init]; [self addSubview:sep]; sep.backgroundColor = RGBA(0, 0, 0, 0.07); self.sepLine = sep; self.backgroundColor = [UIColor clearColor]; self.textLabel.textAlignment = NSTextAlignmentCenter; self.textLabel.font = [UIFont systemFontOfSize:12]; self.textLabel.numberOfLines = 0; self.backgroundColor = [UIColor clearColor]; } - (void)setCourse:(id)course{ _course = course; if ([course nameAttribute] && [course courseName].length) { self.textLabel.attributedText = [[NSAttributedString alloc] initWithString:[course courseName] attributes:[course nameAttribute]]; } else { self.textLabel.text = course.courseName; } } - (void)layoutSubviews{ [super layoutSubviews]; self.sepLine.frame = CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1); } @end @interface GWPCourseListView () /** 顶部选项卡 */ @property (nonatomic, weak) UIView *topBar; /** 顶部选项卡的ContentView */ @property (nonatomic, weak) UIView *topBarContentView; /** 顶部选项卡中的按钮 */ @property (nonatomic, strong) NSArray *topBarBtnArr; /** 时间TableView */ @property (nonatomic, weak) UITableView *timeTableView; /** 上下滚动的ScrollView */ @property (nonatomic, weak) UIScrollView *upDownScrollView; /** 左右滚动的ScrollView */ @property (nonatomic, weak) UIScrollView *leftRightScrollView; /** 课程Table列表 */ @property (nonatomic, strong) NSArray *courseTableArr; /** 亮色集合 */ @property (nonatomic, strong) NSArray *lightColorArr; /** 课程数据 */ @property (nonatomic, strong) NSArray> *courseDataArr; @end @implementation GWPCourseListView #pragma mark - lazy - (NSArray *)lightColorArr{ if (!_lightColorArr) { _lightColorArr = @[ RGBA(39, 201, 155, 1), RGBA(146, 196, 40, 1), RGBA(253, 185, 46, 1), RGBA(112, 161, 246, 1), RGBA(246, 126, 140, 1), RGBA(185, 140, 221, 1), RGBA(30, 180, 235, 1), RGBA(226, 112, 194, 1), ]; } return _lightColorArr; } #pragma mark - init system - (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) { [self setup]; } return self; } - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self setup]; } return self; } - (void)setup{ /*=============================== 初始化变量 ==============================*/ _itemHeight = 50; _timeTableWidth = 50; _courseListWidth = 0; _maxCourseCount = 8; _selectedIndex = 0; // 1-->0 _topBarBgColor = [UIColor whiteColor]; self.backgroundColor = RGBA(245, 245, 245, 1); /*=============================== 添加控件 ==============================*/ NSMutableArray *temp; /** topBar */ UIView *topBar = [[UIView alloc] init]; topBar.backgroundColor = _topBarBgColor; [self addSubview:topBar]; self.topBar = topBar; /** topBarContentView */ UIView *topBarContentView = [[UIView alloc] init]; topBarContentView.backgroundColor = [UIColor clearColor]; topBarContentView.clipsToBounds = YES; [topBar addSubview:topBarContentView]; self.topBarContentView = topBarContentView; /** tabBarBtnArr */ temp = [NSMutableArray array]; for (int i=0; i0) { UIButton *preBtn = self.topBarBtnArr[i-1]; x = CGRectGetMaxX(preBtn.frame); } else { x = -_leftRightScrollView.contentOffset.x; } y=0; if (btn.tag==_selectedIndex) { w = 1.5*courseW; } else { w = courseW; } h = _topBarContentView.frame.size.height; btn.frame = CGRectMake(x, y, w, h); } _upDownScrollView.frame = CGRectMake(0, CGRectGetMaxY(_topBar.frame), width, height-_topBar.frame.size.height); _upDownScrollView.contentSize = CGSizeMake(0, _maxCourseCount*_itemHeight); _timeTableView.frame = CGRectMake(0, 0, _timeTableWidth, _itemHeight*_maxCourseCount); _leftRightScrollView.frame = CGRectMake(_timeTableWidth, 0, width-_timeTableWidth, _timeTableView.frame.size.height); _leftRightScrollView.contentSize = CGSizeMake(courseW*(MaxDay+0.5), 0); for (int i=0; i0) { UITableView *preTable = self.courseTableArr[i-1]; x = CGRectGetMaxX(preTable.frame); } else { x = 0; } y=0; if (table.tag==_selectedIndex) { w = 1.5*courseW; } else { w = courseW; } h = _timeTableWidth*_itemHeight; table.frame = CGRectMake(x, y, w, h); } } #pragma mark - setter - (void)setCourseDataArr:(NSArray> *)courseDataArr{ __block NSUInteger cha = 0; for (int i=0; i<=MaxDay; i++) { NSPredicate *pre = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary * _Nullable bindings) { return evaluatedObject.dayIndex==i; }]; NSArray> *enumCourses = [courseDataArr filteredArrayUsingPredicate:pre]; [enumCourses enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { obj.sortIndex = obj.startCourseIndex-cha; cha = cha + obj.endCourseIndex - obj.startCourseIndex; }]; cha = 0; } _courseDataArr = courseDataArr; } - (void)setDataSource:(id)dataSource{ _dataSource = dataSource; [self loadData]; } - (void)setTopBarBgColor:(UIColor *)topBarBgColor{ _topBarBgColor = topBarBgColor; self.topBar.backgroundColor = topBarBgColor; } #pragma mark - public - (void)reloadData{ [self loadData]; } #pragma mark - private - (void)topBarItemClick:(UIButton *)btn{ _selectedIndex = btn.tag; [self layoutSubviews]; } - (void)loadData{ /*=============================== topBar ==============================*/ NSArray *temp = @[ @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", @"周日" ]; for (int i=0; i _Nonnull evaluatedObject, NSDictionary * _Nullable bindings) { return (tableView.tag==evaluatedObject.dayIndex) && (indexPath.row+1==evaluatedObject.sortIndex); }]; id course = [[self.courseDataArr filteredArrayUsingPredicate:pre] firstObject]; cell.textLabel.textColor = [UIColor whiteColor]; cell.course = course; UIColor *bgColor = [_dataSource courseListView:self courseTitleBackgroundColorForCourse:course]; cell.backgroundColor = course ? (bgColor ? bgColor : self.lightColorArr[arc4random_uniform((u_int32_t)self.lightColorArr.count)]) : [UIColor clearColor]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if ([tableView isEqual:self.timeTableView]) return _itemHeight; NSPredicate *pre = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary * _Nullable bindings) { return (tableView.tag==evaluatedObject.dayIndex) && (indexPath.row+1==evaluatedObject.sortIndex); }]; id course = [[self.courseDataArr filteredArrayUsingPredicate:pre] firstObject]; if (course) { return (course.endCourseIndex-course.startCourseIndex+1)*_itemHeight; } else { return _itemHeight; } // return _itemHeight*2; // return course ? (course.endCourseIndex-course.startCourseIndex)*_itemHeight : _itemHeight; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([tableView isEqual:self.timeTableView]) return; _selectedIndex = tableView.tag; _selectedRow = indexPath.row+1; //NSLog(@"%ld----%ld", _selectedIndex, _selectedRow); [self layoutSubviews]; if ([_delegate respondsToSelector:@selector(courseListView:didSelectedCourse:)]) { CourseCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [_delegate courseListView:self didSelectedCourse:cell.course]; } } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ [self layoutSubviews]; } @end