如何创建这样的日历

时间:2013-06-18 11:46:58

标签: iphone ios6 calendar

我想创建一个基于星期的日历,它应该将UITableView中的日期显示为列表。下面是我发布的图像,以清除所需的输出。经历了很多谷歌,但没有得到任何解决方案。enter image description here。已经走了许多日历KAl,Tapku和Mukhu,但没有得到任何解决方案。请指导。

2 个答案:

答案 0 :(得分:1)

老兄试着这个星期和白天的视图

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 @"";
}

使用数据源显示事件。自定义表格并不是什么大问题,扩展崩溃非常简单。

答案 1 :(得分:0)

我用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

棘手的部分是获取日期信息和自定义。