[英]How to create a calendar like this
我想創建一個基於星期的日歷,它應該將UITableView中的日期顯示為一個列表。 下面是我發布以清除所需輸出的圖像。 已經通過谷歌很多,但沒有任何解決方案。 。 已經走過許多日歷KAL,Tapku和Mukhu,但沒有任何解決方案。 請指導。
杜德(Dude)可以在周和日視圖中嘗試
https://github.com/muhku/calendar-ui ?
周視圖或天視圖可能會讓您入門,或者如果您想從ios EventStore重新開始獲取事件並創建將數據饋入表的數據源。 通常,所有日歷組件都可以這樣做,您甚至可以從上述組件中獲取。
使用以下方法制作日期:
#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
#define CURRENT_CALENDAR [NSCalendar currentCalendar]
+ (NSDate *)nextDayFromDate:(NSDate *)date {
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:date];
[components setDay:[components day] + 1];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [CURRENT_CALENDAR dateFromComponents:components];
}
+ (NSDate *)previousDayFromDate:(NSDate *)date {
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:date];
[components setDay:[components day] - 1];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [CURRENT_CALENDAR dateFromComponents:components];
}
將日期組織成一周-將這些日期分組以組成一周。 使用此方法按天數取星期幾:
+ (NSString *)dayNameForWeekDay:(int)weekday
{
switch (weekday) {
case 1:
return @"Sunday";
break;
case 2:
return @"Monday";
break;
case 3:
return @"Tuesday";
break;
case 4:
return @"Wednesday";
break;
case 5:
return @"Thursday";
break;
case 6:
return @"Friday";
break;
case 7:
return @"Saturday";
break;
default:
break;
}
return @"";
}
並使用數據源顯示事件。 自定義表格並不重要,展開折疊非常簡單。
我使用tableview粗化了一些東西。 您正在尋找的基本行為是在選擇日期時添加行(並隱藏以前選擇的行)。 我每天做一個tableView並有一個部分,並在其下添加事件。
我從xib添加了TableView,但應該在此設置的代碼中完成此操作。
//
// TCViewController.m
// TableCalendarTest
//
// Created by Brian Broom on 6/18/13.
// Copyright (c) 2013 Brian Broom. All rights reserved.
//
#import "TCViewController.h"
@interface TCViewController ()
{
int selectedSection;
}
@end
@implementation TCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == selectedSection) {
return 3;
} else {
return 1;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
[tableView beginUpdates];
[self.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:selectedSection] animated:YES];
NSMutableArray *oldRows = [[NSMutableArray alloc] init];
[oldRows addObject:[NSIndexPath indexPathForRow:1 inSection:selectedSection]];
[oldRows addObject:[NSIndexPath indexPathForRow:2 inSection:selectedSection]];
selectedSection = indexPath.section;
[tableView deleteRowsAtIndexPaths:oldRows withRowAnimation:UITableViewRowAnimationTop];
NSMutableArray *newRows = [[NSMutableArray alloc] init];
[newRows addObject:[NSIndexPath indexPathForRow:1 inSection:selectedSection]];
[newRows addObject:[NSIndexPath indexPathForRow:2 inSection:selectedSection]];
[tableView insertRowsAtIndexPaths:newRows withRowAnimation:UITableViewRowAnimationBottom];
[tableView endUpdates];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0) {
[cell.textLabel setText:[NSString stringWithFormat:@"Day"]];
} else {
[cell.textLabel setText:[NSString stringWithFormat:@"Event %d", indexPath.row]];
}
return cell;
}
@end
棘手的部分是獲取日期信息和自定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.