Skip to content

Commit

Permalink
adding weekday header cells for each month
Browse files Browse the repository at this point in the history
  • Loading branch information
min committed Jul 28, 2013
1 parent 3594adb commit 5fb9d64
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 58 deletions.
12 changes: 12 additions & 0 deletions MNCalendarView.xcodeproj/project.pbxproj
Expand Up @@ -24,6 +24,8 @@
39A860E517A3229F003F6F4A /* NSDate+MNAdditions.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 39A860D817A2F234003F6F4A /* NSDate+MNAdditions.h */; };
39A860E617A3229F003F6F4A /* MNFastDateEnumeration.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 39A860E117A31FAB003F6F4A /* MNFastDateEnumeration.h */; };
39A860EA17A32E79003F6F4A /* MNCalendarHeaderView.m in Sources */ = {isa = PBXBuildFile; fileRef = 39A860E917A32E79003F6F4A /* MNCalendarHeaderView.m */; };
39FCA69017A5CE0D0064F303 /* MNCalendarViewWeekdayCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 39FCA68F17A5CE0D0064F303 /* MNCalendarViewWeekdayCell.m */; };
39FCA69617A5D0F70064F303 /* MNCalendarViewDayCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 39FCA69517A5D0F70064F303 /* MNCalendarViewDayCell.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -75,6 +77,10 @@
39A860E217A31FAB003F6F4A /* MNFastDateEnumeration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MNFastDateEnumeration.m; sourceTree = "<group>"; };
39A860E817A32E79003F6F4A /* MNCalendarHeaderView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MNCalendarHeaderView.h; sourceTree = "<group>"; };
39A860E917A32E79003F6F4A /* MNCalendarHeaderView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MNCalendarHeaderView.m; sourceTree = "<group>"; };
39FCA68E17A5CE0D0064F303 /* MNCalendarViewWeekdayCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MNCalendarViewWeekdayCell.h; sourceTree = "<group>"; };
39FCA68F17A5CE0D0064F303 /* MNCalendarViewWeekdayCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MNCalendarViewWeekdayCell.m; sourceTree = "<group>"; };
39FCA69417A5D0F70064F303 /* MNCalendarViewDayCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MNCalendarViewDayCell.h; sourceTree = "<group>"; };
39FCA69517A5D0F70064F303 /* MNCalendarViewDayCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MNCalendarViewDayCell.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -138,8 +144,12 @@
3907541A179F6DC5002AC1EC /* MNCalendarView.m */,
39A860DB17A30210003F6F4A /* MNCalendarViewCell.h */,
39A860DC17A30210003F6F4A /* MNCalendarViewCell.m */,
39FCA69417A5D0F70064F303 /* MNCalendarViewDayCell.h */,
39FCA69517A5D0F70064F303 /* MNCalendarViewDayCell.m */,
394051CF17A350850099F12A /* MNCalendarViewLayout.h */,
394051D017A350850099F12A /* MNCalendarViewLayout.m */,
39FCA68E17A5CE0D0064F303 /* MNCalendarViewWeekdayCell.h */,
39FCA68F17A5CE0D0064F303 /* MNCalendarViewWeekdayCell.m */,
39A860E117A31FAB003F6F4A /* MNFastDateEnumeration.h */,
39A860E217A31FAB003F6F4A /* MNFastDateEnumeration.m */,
39A860D817A2F234003F6F4A /* NSDate+MNAdditions.h */,
Expand Down Expand Up @@ -274,7 +284,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
39FCA69617A5D0F70064F303 /* MNCalendarViewDayCell.m in Sources */,
39A860E317A31FAB003F6F4A /* MNFastDateEnumeration.m in Sources */,
39FCA69017A5CE0D0064F303 /* MNCalendarViewWeekdayCell.m in Sources */,
394051D117A350850099F12A /* MNCalendarViewLayout.m in Sources */,
3907541B179F6DC5002AC1EC /* MNCalendarView.m in Sources */,
39A860DD17A30210003F6F4A /* MNCalendarViewCell.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion MNCalendarView/MNCalendarHeaderView.m
Expand Up @@ -14,7 +14,7 @@ @implementation MNCalendarHeaderView

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = UIColor.whiteColor;
self.backgroundColor = UIColor.redColor;
}
return self;
}
Expand Down
48 changes: 32 additions & 16 deletions MNCalendarView/MNCalendarView.m
Expand Up @@ -8,19 +8,21 @@

#import "MNCalendarView.h"
#import "MNCalendarViewLayout.h"
#import "MNCalendarViewCell.h"
#import "MNCalendarViewDayCell.h"
#import "MNCalendarViewWeekdayCell.h"
#import "MNCalendarHeaderView.h"


#import "NSDate+MNAdditions.h"
#import "MNFastDateEnumeration.h"

@interface MNCalendarView() <UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic,strong,readwrite) UICollectionView *collectionView;
@property(nonatomic,strong,readwrite) UICollectionView *collectionView;
@property(nonatomic,strong,readwrite) UICollectionViewFlowLayout *layout;

@property(nonatomic,strong,readwrite) NSArray *monthDates;
@property(nonatomic,assign,readwrite) NSUInteger daysInWeek;
@property(nonatomic,strong,readwrite) NSArray *monthDates;
@property(nonatomic,assign,readwrite) NSUInteger daysInWeek;

- (NSDate *)firstVisibleDateOfMonth:(NSDate *)date;
- (NSDate *)lastVisibleDateOfMonth:(NSDate *)date;
Expand Down Expand Up @@ -56,8 +58,11 @@ - (UICollectionView *)collectionView {
_collectionView.dataSource = self;
_collectionView.delegate = self;

[_collectionView registerClass:MNCalendarViewCell.class
forCellWithReuseIdentifier:MNCalendarViewCellIdentifier];
[_collectionView registerClass:MNCalendarViewDayCell.class
forCellWithReuseIdentifier:MNCalendarViewDayCellIdentifier];

[_collectionView registerClass:MNCalendarViewWeekdayCell.class
forCellWithReuseIdentifier:MNCalendarViewWeekdayCellIdentifier];

[_collectionView registerClass:MNCalendarHeaderView.class
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
Expand Down Expand Up @@ -126,7 +131,9 @@ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
return self.monthDates.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
MNCalendarHeaderView *headerView =
[collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:MNCalendarHeaderViewIdentifier
Expand All @@ -138,28 +145,37 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe
NSDate *monthDate = self.monthDates[section];

NSDateComponents *components =
[self.calendar components:NSDayCalendarUnit
fromDate:[self firstVisibleDateOfMonth:monthDate]
toDate:[self lastVisibleDateOfMonth:monthDate]
options:0];
[self.calendar components:NSDayCalendarUnit
fromDate:[self firstVisibleDateOfMonth:monthDate]
toDate:[self lastVisibleDateOfMonth:monthDate]
options:0];

return components.day + 1;
return self.daysInWeek + components.day + 1;
}

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

MNCalendarViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:MNCalendarViewCellIdentifier
forIndexPath:indexPath];
if (indexPath.item < self.daysInWeek) {
MNCalendarViewWeekdayCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:MNCalendarViewWeekdayCellIdentifier
forIndexPath:indexPath];

cell.separatorColor = self.separatorColor;
return cell;
}
MNCalendarViewDayCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:MNCalendarViewDayCellIdentifier
forIndexPath:indexPath];

cell.separatorColor = self.separatorColor;

NSDate *monthDate = self.monthDates[indexPath.section];
NSDate *firstDateInMonth =
[self firstVisibleDateOfMonth:monthDate];

[cell setDate:[firstDateInMonth dateByAddingTimeInterval:indexPath.item * MN_DAY]
NSUInteger day = indexPath.item - self.daysInWeek;
[cell setDate:[firstDateInMonth dateByAddingTimeInterval:day * MN_DAY]
month:monthDate
calendar:self.calendar];

Expand Down
10 changes: 2 additions & 8 deletions MNCalendarView/MNCalendarViewCell.h
Expand Up @@ -8,20 +8,14 @@

#import <UIKit/UIKit.h>

extern NSString *const MNCalendarViewCellIdentifier;

@interface MNCalendarViewCell : UICollectionViewCell

@property(nonatomic,strong,readonly) NSDate *date;
@property(nonatomic,strong,readonly) NSDate *month;
@property(nonatomic,strong,readonly) NSCalendar *calendar;
@property(nonatomic,strong) NSCalendar *calendar;

@property(nonatomic,assign,getter = isEnabled) BOOL enabled;

@property(nonatomic,strong) UIColor *separatorColor;

- (void)setDate:(NSDate *)date
month:(NSDate *)month
calendar:(NSCalendar *)calendar;
@property(nonatomic,strong,readonly) UILabel *titleLabel;

@end
33 changes: 0 additions & 33 deletions MNCalendarView/MNCalendarViewCell.m
Expand Up @@ -24,10 +24,6 @@ @interface MNCalendarViewCell()

@property(nonatomic,strong,readwrite) UILabel *titleLabel;

@property(nonatomic,strong,readwrite) NSDate *date;
@property(nonatomic,strong,readwrite) NSDate *month;
@property(nonatomic,strong,readwrite) NSCalendar *calendar;

@end

@implementation MNCalendarViewCell
Expand Down Expand Up @@ -61,35 +57,6 @@ - (void)layoutSubviews {
self.selectedBackgroundView.frame = self.bounds;
}

- (void)setDate:(NSDate *)date
month:(NSDate *)month
calendar:(NSCalendar *)calendar {

self.date = date;
self.month = month;
self.calendar = calendar;

NSDateComponents *components =
[self.calendar components:NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:self.date];

NSDateComponents *monthComponents =
[self.calendar components:NSMonthCalendarUnit
fromDate:self.month];

self.titleLabel.text = [NSString stringWithFormat:@"%d", components.day];
self.enabled = monthComponents.month == components.month;

[self setNeedsDisplay];
}

- (void)setEnabled:(BOOL)enabled {
_enabled = enabled;

self.titleLabel.textColor =
self.enabled ? UIColor.darkTextColor : UIColor.lightGrayColor;
}

- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();

Expand Down
22 changes: 22 additions & 0 deletions MNCalendarView/MNCalendarViewDayCell.h
@@ -0,0 +1,22 @@
//
// MNCalendarViewDayCell.h
// MNCalendarView
//
// Created by Min Kim on 7/28/13.
// Copyright (c) 2013 min. All rights reserved.
//

#import "MNCalendarViewCell.h"

extern NSString *const MNCalendarViewDayCellIdentifier;

@interface MNCalendarViewDayCell : MNCalendarViewCell

@property(nonatomic,strong,readonly) NSDate *date;
@property(nonatomic,strong,readonly) NSDate *month;

- (void)setDate:(NSDate *)date
month:(NSDate *)month
calendar:(NSCalendar *)calendar;

@end
54 changes: 54 additions & 0 deletions MNCalendarView/MNCalendarViewDayCell.m
@@ -0,0 +1,54 @@
//
// MNCalendarViewDayCell.m
// MNCalendarView
//
// Created by Min Kim on 7/28/13.
// Copyright (c) 2013 min. All rights reserved.
//

#import "MNCalendarViewDayCell.h"

NSString *const MNCalendarViewDayCellIdentifier = @"MNCalendarViewDayCellIdentifier";

@interface MNCalendarViewDayCell()

@property(nonatomic,strong,readwrite) NSDate *date;
@property(nonatomic,strong,readwrite) NSDate *month;

@end

@implementation MNCalendarViewDayCell

- (void)setDate:(NSDate *)date
month:(NSDate *)month
calendar:(NSCalendar *)calendar {

self.date = date;
self.month = month;
self.calendar = calendar;

NSDateComponents *components =
[self.calendar components:NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:self.date];

NSDateComponents *monthComponents =
[self.calendar components:NSMonthCalendarUnit
fromDate:self.month];

self.titleLabel.text = [NSString stringWithFormat:@"%d", components.day];
self.enabled = monthComponents.month == components.month;

[self setNeedsDisplay];
}

- (void)setEnabled:(BOOL)enabled {
[super setEnabled:enabled];

self.titleLabel.textColor =
self.enabled ? UIColor.darkTextColor : UIColor.lightGrayColor;

self.backgroundColor =
self.enabled ? UIColor.whiteColor : [UIColor colorWithRed:.96f green:.96f blue:.96f alpha:1.f];
}

@end
17 changes: 17 additions & 0 deletions MNCalendarView/MNCalendarViewWeekdayCell.h
@@ -0,0 +1,17 @@
//
// MNCalendarWeekdayViewCell.h
// MNCalendarView
//
// Created by Min Kim on 7/28/13.
// Copyright (c) 2013 min. All rights reserved.
//

#import "MNCalendarViewCell.h"

extern NSString *const MNCalendarViewWeekdayCellIdentifier;

@interface MNCalendarViewWeekdayCell : MNCalendarViewCell

@property(nonatomic,assign) NSUInteger weekday;

@end
21 changes: 21 additions & 0 deletions MNCalendarView/MNCalendarViewWeekdayCell.m
@@ -0,0 +1,21 @@
//
// MNCalendarWeekdayViewCell.m
// MNCalendarView
//
// Created by Min Kim on 7/28/13.
// Copyright (c) 2013 min. All rights reserved.
//

#import "MNCalendarViewWeekdayCell.h"

NSString *const MNCalendarViewWeekdayCellIdentifier = @"MNCalendarViewWeekdayCellIdentifier";

@implementation MNCalendarViewWeekdayCell

- (void)setWeekday:(NSUInteger)weekday {
_weekday = weekday;

[self setNeedsDisplay];
}

@end

0 comments on commit 5fb9d64

Please sign in to comment.