Skip to content

Commit

Permalink
Fix for calendar grid height issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
devinross committed Apr 24, 2012
1 parent d74d48b commit dbb3b9c
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 64 deletions.
9 changes: 3 additions & 6 deletions demo/iPhone/DemoCalendarMonth.m
Expand Up @@ -36,14 +36,11 @@ @implementation DemoCalendarMonth
@synthesize dataArray, dataDictionary; @synthesize dataArray, dataDictionary;






- (void) viewDidLoad{ - (void) viewDidLoad{
[super viewDidLoad]; [super viewDidLoad];
[self.monthView selectDate:[NSDate month]]; [self.monthView selectDate:[NSDate month]];


} }

- (void) viewDidAppear:(BOOL)animated{ - (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated]; [super viewDidAppear:animated];


Expand Down Expand Up @@ -78,16 +75,16 @@ - (void) calendarMonthView:(TKCalendarMonthView*)mv monthDidChange:(NSDate*)d an
} }




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1; return 1;


} }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *ar = [dataDictionary objectForKey:[self.monthView dateSelected]]; NSArray *ar = [dataDictionary objectForKey:[self.monthView dateSelected]];
if(ar == nil) return 0; if(ar == nil) return 0;
return [ar count]; return [ar count];
} }
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { - (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"Cell"; static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier]; UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
Expand Down
10 changes: 2 additions & 8 deletions demo/iPhone/RootViewController.m
Expand Up @@ -140,7 +140,7 @@ - (void) tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)inde
} }


if(s==0&&r==1) if(s==0&&r==1)
vc = [[DemoCalendarMonth alloc] initWithSunday:NO]; vc = [[DemoCalendarMonth alloc] initWithSunday:YES];






Expand All @@ -156,13 +156,7 @@ - (void) tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)inde
vc = [[MapViewController alloc] init]; vc = [[MapViewController alloc] init];




/*
else if(s==2 && r==0)
vc = [[DemoCalendarMonth alloc] initWithSunday:YES];
else if(s==2 && r==1)
vc = [[TKCalendarDayViewController alloc] init];
*/

else if(s==2 && r==0) else if(s==2 && r==0)
vc = [[LabelViewController alloc] initWithStyle:UITableViewStyleGrouped]; vc = [[LabelViewController alloc] initWithStyle:UITableViewStyleGrouped];
else if(s==2 && r==1) else if(s==2 && r==1)
Expand Down
Binary file not shown.
4 changes: 2 additions & 2 deletions src/TapkuLibrary/TKCalendarMonthTableViewController.m
Expand Up @@ -47,12 +47,12 @@ - (void) loadView{
float y,height; float y,height;
y = self.monthView.frame.origin.y + self.monthView.frame.size.height; y = self.monthView.frame.origin.y + self.monthView.frame.size.height;
height = self.view.frame.size.height - y; height = self.view.frame.size.height - y;



_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, self.view.bounds.size.width, height) style:UITableViewStylePlain]; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, self.view.bounds.size.width, height) style:UITableViewStylePlain];
_tableView.delegate = self; _tableView.delegate = self;
_tableView.dataSource = self; _tableView.dataSource = self;
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin; _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_tableView]; [self.view addSubview:_tableView];
[self.view sendSubviewToBack:_tableView]; [self.view sendSubviewToBack:_tableView];
} }
Expand Down
57 changes: 12 additions & 45 deletions src/TapkuLibrary/TKCalendarMonthView.m
Expand Up @@ -93,9 +93,6 @@ - (NSDate*) previousMonth{


} }





- (NSDate*) lastOfMonthDate { - (NSDate*) lastOfMonthDate {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:self]; NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:self];
Expand All @@ -122,13 +119,10 @@ + (NSDate*) lastOfCurrentMonth{
return [gregorian dateFromComponents:comp]; return [gregorian dateFromComponents:comp];
} }




@end @end




#pragma mark - #pragma mark -

@interface TKCalendarMonthTiles : UIView { @interface TKCalendarMonthTiles : UIView {


id target; id target;
Expand Down Expand Up @@ -160,19 +154,15 @@ + (NSArray*) rangeOfDatesInMonthGrid:(NSDate*)date startOnSunday:(BOOL)sunday;
@end @end


#pragma mark - #pragma mark -

#define dotFontSize 18.0 #define dotFontSize 18.0
#define dateFontSize 22.0 #define dateFontSize 22.0

@interface TKCalendarMonthTiles (private) @interface TKCalendarMonthTiles (private)

@property (strong,nonatomic) UIImageView *selectedImageView; @property (strong,nonatomic) UIImageView *selectedImageView;
@property (strong,nonatomic) UILabel *currentDay; @property (strong,nonatomic) UILabel *currentDay;
@property (strong,nonatomic) UILabel *dot; @property (strong,nonatomic) UILabel *dot;
@end @end


#pragma mark - #pragma mark -

@implementation TKCalendarMonthTiles @implementation TKCalendarMonthTiles
@synthesize monthDate; @synthesize monthDate;


Expand Down Expand Up @@ -267,24 +257,28 @@ - (id) initWithMonth:(NSDate*)date marks:(NSArray*)markArray startDayOnSunday:(B
monthDate = date; monthDate = date;
startOnSunday = sunday; startOnSunday = sunday;




TKDateInformation dateInfo = [monthDate dateInformationWithTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; TKDateInformation dateInfo = [monthDate dateInformationWithTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
firstWeekday = dateInfo.weekday; firstWeekday = dateInfo.weekday;




NSDate *prev = [monthDate previousMonth]; NSDate *prev = [monthDate previousMonth];
//NSDate *next = [monthDate nextMonth];

daysInMonth = [[monthDate nextMonth] daysBetweenDate:monthDate]; daysInMonth = [[monthDate nextMonth] daysBetweenDate:monthDate];


/*
int row = (daysInMonth + dateInfo.weekday - 1); int row = (daysInMonth + dateInfo.weekday - 1);
if(dateInfo.weekday==1&&!sunday) row = daysInMonth + 6; if(dateInfo.weekday==1&&!sunday) row = daysInMonth + 6;
if(!sunday) row--; if(!sunday) row--;
row = (row / 7) + ((row % 7 == 0) ? 0:1); row = (row / 7) + ((row % 7 == 0) ? 0:1);
float h = 44 * row; float h = 44 * row;
*/

NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:date startOnSunday:sunday];
NSUInteger numberOfDaysBetween = [[dates objectAtIndex:0] daysBetweenDate:[dates lastObject]];
NSUInteger scale = (numberOfDaysBetween / 7) + 1;
CGFloat h = 44.0f * scale;



TKDateInformation todayInfo = [[NSDate date] dateInformation]; TKDateInformation todayInfo = [[NSDate date] dateInformation];
today = dateInfo.month == todayInfo.month && dateInfo.year == todayInfo.year ? todayInfo.day : -5; today = dateInfo.month == todayInfo.month && dateInfo.year == todayInfo.year ? todayInfo.day : -5;
Expand All @@ -301,13 +295,10 @@ - (id) initWithMonth:(NSDate*)date marks:(NSArray*)markArray startDayOnSunday:(B
firstOfPrev = preDayCnt - firstWeekday+3; firstOfPrev = preDayCnt - firstWeekday+3;
} }
lastOfPrev = preDayCnt; lastOfPrev = preDayCnt;

} }





self.frame = CGRectMake(0, 1.0, 320.0f, h+1);

self.frame = CGRectMake(0, 1, 320, h+1);


[self.selectedImageView addSubview:self.currentDay]; [self.selectedImageView addSubview:self.currentDay];
[self.selectedImageView addSubview:self.dot]; [self.selectedImageView addSubview:self.dot];
Expand Down Expand Up @@ -490,7 +481,6 @@ - (NSDate*) dateSelected{
} }





- (void) reactToTouch:(UITouch*)touch down:(BOOL)down{ - (void) reactToTouch:(UITouch*)touch down:(BOOL)down{


CGPoint p = [touch locationInView:self]; CGPoint p = [touch locationInView:self];
Expand Down Expand Up @@ -624,17 +614,16 @@ - (UIImageView *) selectedImageView{


@end @end


#pragma mark -


@interface TKCalendarMonthView (private)


#pragma mark -
@interface TKCalendarMonthView (private)
@property (strong,nonatomic) UIScrollView *tileBox; @property (strong,nonatomic) UIScrollView *tileBox;
@property (strong,nonatomic) UIImageView *topBackground; @property (strong,nonatomic) UIImageView *topBackground;
@property (strong,nonatomic) UILabel *monthYear; @property (strong,nonatomic) UILabel *monthYear;
@property (strong,nonatomic) UIButton *leftArrow; @property (strong,nonatomic) UIButton *leftArrow;
@property (strong,nonatomic) UIButton *rightArrow; @property (strong,nonatomic) UIButton *rightArrow;
@property (strong,nonatomic) UIImageView *shadow; @property (strong,nonatomic) UIImageView *shadow;

@end @end


#pragma mark - #pragma mark -
Expand All @@ -651,19 +640,12 @@ - (id) initWithSundayAsFirst:(BOOL)s{
self.backgroundColor = [UIColor grayColor]; self.backgroundColor = [UIColor grayColor];


sunday = s; sunday = s;



currentTile = [[TKCalendarMonthTiles alloc] initWithMonth:[[NSDate date] firstOfMonth] marks:nil startDayOnSunday:sunday]; currentTile = [[TKCalendarMonthTiles alloc] initWithMonth:[[NSDate date] firstOfMonth] marks:nil startDayOnSunday:sunday];
[currentTile setTarget:self action:@selector(tile:)]; [currentTile setTarget:self action:@selector(tile:)];


CGRect r = CGRectMake(0, 0, self.tileBox.bounds.size.width, self.tileBox.bounds.size.height + self.tileBox.frame.origin.y); CGRect r = CGRectMake(0, 0, self.tileBox.bounds.size.width, self.tileBox.bounds.size.height + self.tileBox.frame.origin.y);


self.frame = r; self.frame = r;




[self addSubview:self.topBackground]; [self addSubview:self.topBackground];
[self.tileBox addSubview:currentTile]; [self.tileBox addSubview:currentTile];
[self addSubview:self.tileBox]; [self addSubview:self.tileBox];
Expand Down Expand Up @@ -715,16 +697,12 @@ - (id) initWithSundayAsFirst:(BOOL)s{
sund.day = 11; sund.day = 11;
NSString *sat = [dateFormat stringFromDate:[NSDate dateFromDateInformation:sund timeZone:tz]]; NSString *sat = [dateFormat stringFromDate:[NSDate dateFromDateInformation:sund timeZone:tz]];





NSArray *ar; NSArray *ar;
if(sunday) ar = [NSArray arrayWithObjects:sun,mon,tue,wed,thu,fri,sat,nil]; if(sunday) ar = [NSArray arrayWithObjects:sun,mon,tue,wed,thu,fri,sat,nil];
else ar = [NSArray arrayWithObjects:mon,tue,wed,thu,fri,sat,sun,nil]; else ar = [NSArray arrayWithObjects:mon,tue,wed,thu,fri,sat,sun,nil];


int i = 0; int i = 0;
for(NSString *s in ar){ for(NSString *s in ar){

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(46 * i, 29, 46, 15)]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(46 * i, 29, 46, 15)];
[self addSubview:label]; [self addSubview:label];
label.text = s; label.text = s;
Expand All @@ -734,7 +712,6 @@ - (id) initWithSundayAsFirst:(BOOL)s{
label.font = [UIFont systemFontOfSize:11]; label.font = [UIFont systemFontOfSize:11];
label.backgroundColor = [UIColor clearColor]; label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:59/255. green:73/255. blue:88/255. alpha:1]; label.textColor = [UIColor colorWithRed:59/255. green:73/255. blue:88/255. alpha:1];

i++; i++;
} }


Expand Down Expand Up @@ -867,7 +844,6 @@ - (NSDate*) monthDate{
return [currentTile monthDate]; return [currentTile monthDate];
} }
- (void) selectDate:(NSDate*)date{ - (void) selectDate:(NSDate*)date{
//TKDateInformation info = [date dateInformation];
TKDateInformation info = [date dateInformationWithTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; TKDateInformation info = [date dateInformationWithTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate *month = [date firstOfMonth]; NSDate *month = [date firstOfMonth];


Expand Down Expand Up @@ -986,12 +962,7 @@ - (UIButton *) leftArrow{
leftArrow = [UIButton buttonWithType:UIButtonTypeCustom]; leftArrow = [UIButton buttonWithType:UIButtonTypeCustom];
leftArrow.tag = 0; leftArrow.tag = 0;
[leftArrow addTarget:self action:@selector(changeMonth:) forControlEvents:UIControlEventTouchUpInside]; [leftArrow addTarget:self action:@selector(changeMonth:) forControlEvents:UIControlEventTouchUpInside];




[leftArrow setImage:[UIImage imageNamedTK:@"TapkuLibrary.bundle/Images/calendar/Month Calendar Left Arrow"] forState:0]; [leftArrow setImage:[UIImage imageNamedTK:@"TapkuLibrary.bundle/Images/calendar/Month Calendar Left Arrow"] forState:0];

leftArrow.frame = CGRectMake(0, 0, 48, 38); leftArrow.frame = CGRectMake(0, 0, 48, 38);
} }
return leftArrow; return leftArrow;
Expand All @@ -1002,11 +973,7 @@ - (UIButton *) rightArrow{
rightArrow.tag = 1; rightArrow.tag = 1;
[rightArrow addTarget:self action:@selector(changeMonth:) forControlEvents:UIControlEventTouchUpInside]; [rightArrow addTarget:self action:@selector(changeMonth:) forControlEvents:UIControlEventTouchUpInside];
rightArrow.frame = CGRectMake(320-45, 0, 48, 38); rightArrow.frame = CGRectMake(320-45, 0, 48, 38);



[rightArrow setImage:[UIImage imageNamedTK:@"TapkuLibrary.bundle/Images/calendar/Month Calendar Right Arrow"] forState:0]; [rightArrow setImage:[UIImage imageNamedTK:@"TapkuLibrary.bundle/Images/calendar/Month Calendar Right Arrow"] forState:0];

} }
return rightArrow; return rightArrow;
} }
Expand Down
4 changes: 1 addition & 3 deletions src/TapkuLibrary/TKCalendarMonthViewController.h
Expand Up @@ -37,9 +37,7 @@




/** The `TKCalendarMonthViewController` class creates a controller object that manages a calendar month grid. */ /** The `TKCalendarMonthViewController` class creates a controller object that manages a calendar month grid. */
@interface TKCalendarMonthViewController : UIViewController <TKCalendarMonthViewDelegate,TKCalendarMonthViewDataSource> { @interface TKCalendarMonthViewController : UIViewController <TKCalendarMonthViewDelegate,TKCalendarMonthViewDataSource>
BOOL _sundayFirst;
}


/** Initializes a month view controller to manage a month grid. Sunday will be the left-most day. /** Initializes a month view controller to manage a month grid. Sunday will be the left-most day.
@return A newly create month view controller. @return A newly create month view controller.
Expand Down
6 changes: 6 additions & 0 deletions src/TapkuLibrary/TKCalendarMonthViewController.m
Expand Up @@ -32,6 +32,12 @@
#import "TKCalendarMonthView.h" #import "TKCalendarMonthView.h"




@interface TKCalendarMonthViewController () {
BOOL _sundayFirst;
}

@end

@implementation TKCalendarMonthViewController @implementation TKCalendarMonthViewController
@synthesize monthView = _monthView; @synthesize monthView = _monthView;


Expand Down

0 comments on commit dbb3b9c

Please sign in to comment.