【本を書きました!】iOSプログラミング逆引きリファレンス108
会社の同僚といっしょに本を書きました。
iOSアプリ開発中に遭遇するよくある疑問をレシピ形式でまとめた本です。iOS4の新機能も満載です。iOSアプリ開発のお供にどうぞ!
2010-07-12
■[iphone]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
トラックバック - http://d.hatena.ne.jp/thata/20100712/1278864244
リンク元
- 49 http://www.google.co.jp/search?q=NSMutableArray&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:ja:official&hl=ja&client=firefox-a
- 45 http://www.google.co.jp/search?client=safari&rls=en&q=NSMutableArray&ie=UTF-8&oe=UTF-8&redir_esc=&ei=GNo6TO7HNYzCcc7wzfoO
- 27 http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=NSMutableArray
- 25 http://www.google.co.jp/search?hl=ja&lr=lang_ja&tbs=lr:lang_1ja&q=curl+put+xml&aq=f&aqi=&aql=&oq=&gs_rfai=
- 24 http://www.google.co.jp/search?hl=ja&source=hp&q=nsmutablearray&aq=f&aqi=&aql=&oq=&gs_rfai=CsihhDFE6TMevKYzEiQOP4fjFCQAAAKoEBU_Q5aVP
- 23 http://www.google.co.jp/search?client=safari&rls=en&q=NSArray&ie=UTF-8&oe=UTF-8&redir_esc=&ei=iYk6TKWhB83QceDC7foO
- 23 http://www.google.co.jp/search?hl=ja&client=safari&rls=en&q=iphone+ダイアログ&aq=f&aqi=g1&aql=&oq=&gs_rfai=
- 21 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4ADFA_jaJP374JP374&q=iphone+@property+nonatomic
- 18 http://www.google.co.jp/search?hl=ja&q=NSMutableArray&aq=f&aqi=&aql=&oq=&gs_rfai=
- 17 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja-JP-mac:official&q=iphone+xib+file+owner&aq=f&aqi=&aql=&oq=&gs_rfai=

