[英]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.