TapkuLibraryのカレンダーを使ってみた

自分用のメモとして、とりあえずコードだけ貼っておく。「+」ボタンを押すとメモに「とんかつ」と記録できるカレンダーです。

#import <UIKit/UIKit.h>
#import "TapkuLibrary/TapkuLibrary.h"

@interface MyCalendarViewController : TKCalendarMonthTableViewController {
	NSMutableDictionary	*memoDictionary;
}

@end
#import "MyCalendarViewController.h"

@implementation MyCalendarViewController

- (void)didAddButtonPush {
	NSDate *date = [self.monthView dateSelected];
	if (date) {
		NSMutableArray *memos = [memoDictionary objectForKey:date];
		if (!memos) {
			memos = [NSMutableArray array];
			[memoDictionary setObject:memos forKey:date];
		}
		[memos addObject:@"とんかつ"];
	}
	
	// カレンダーとテーブルをリロード
	[self.monthView reload];
	[self.tableView reloadData];
}

#pragma mark カレンダー関連

// 選択した期間内でのドットマークの有無を返す
- (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate{
	NSMutableArray *marks = [NSMutableArray array];
	
	NSDate *d = startDate;
	while (YES) {
		// 終了判定
		if ([d compare:lastDate] == NSOrderedDescending) {
			break;
		}
		
		// メモがある場合にはYESを、無い場合にはNOをセットする
		id memos = [memoDictionary objectForKey:d];
		if (memos) {
			[marks addObject:[NSNumber numberWithBool:YES]];
		} else {
			[marks addObject:[NSNumber numberWithBool:NO]];
		}
		
		// 日付を1日すすめる
		TKDateInformation dateInfo = [d dateInformation];
		dateInfo.day++;
		d = [NSDate dateFromDateInformation:dateInfo];
	}
	
	return marks;
}

- (void) calendarMonthView:(TKCalendarMonthView*)monthView didSelectDate:(NSDate*)d{
	[self.tableView reloadData];
}

- (void) calendarMonthView:(TKCalendarMonthView*)mv monthDidChange:(NSDate*)d{
	[super calendarMonthView:mv monthDidChange:d];
	[self.tableView reloadData];
}


#pragma mark カレンダーテーブル関連

// セクション数は常に1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// 選択した日のメモの個数を返す
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSDate *date = [self.monthView dateSelected];
	if (date) {
		NSMutableArray *memos = [memoDictionary objectForKey:date];
		if (memos) {
			return [memos count];
		} else {
			return 0;
		}
	} else {
		return 0;
	}
}

// 選択した日のメモの書かれたセルを返す
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    
    // メモを返す
	NSArray *memos = [memoDictionary objectForKey:[monthView dateSelected]];
	cell.textLabel.text = [memos objectAtIndex:indexPath.row];
	
    return cell;
}

#pragma mark UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	
	// メモを初期化
	memoDictionary = [[NSMutableDictionary alloc] init];
	
	// 追加ボタンをナビゲーションバーへ
	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] 
								  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 																						   
								  target:self  								  																		   
								  action:@selector(didAddButtonPush)];
	self.navigationItem.rightBarButtonItem = addButton;
}

- (void)dealloc {
	[memoDictionary release];
    [super dealloc];
}


@end