简体   繁体   English

如何创建这样的日历

[英]How to create a calendar like this

I want to create a week based calendar, it should show the days in UITableView as a list. 我想创建一个基于星期的日历,它应该将UITableView中的日期显示为一个列表。 Below is image i have posted to clear the required output. 下面是我发布以清除所需输出的图像。 Have gone through google a lot, but doesn't got any solution. 已经通过谷歌很多,但没有任何解决方案。 在此处输入图片说明 . Have gone throgh many calendars KAl, Tapku and also Mukhu but not got any solution for it. 已经走过许多日历KAL,Tapku和Mukhu,但没有任何解决方案。 Please guide. 请指导。

Dude try this for week and day view 杜德(Dude)可以在周和日视图中尝试

https://github.com/muhku/calendar-ui ? https://github.com/muhku/calendar-ui

week or day view might get you started with or if you wan to start afresh fetch events from the ios EventStore and make a datasource that feeds data to your table. 周视图或天视图可能会让您入门,或者如果您想从ios EventStore重新开始获取事件并创建将数据馈入表的数据源。 Mostly all calendar components do that, you can even take that from the above component. 通常,所有日历组件都可以这样做,您甚至可以从上述组件中获取。

Use these methods to make dates: 使用以下方法制作日期:

#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];
}

Organise dates into a week - group those dates to form a week. 将日期组织成一周-将这些日期分组以组成一周。 Take day of week by day number using this method: 使用此方法按天数取星期几:

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

And using the datasource show the events. 并使用数据源显示事件。 Customizing your table is not a big deal, expanding collapsing is so simple. 自定义表格并不重要,展开折叠非常简单。

I roughed out something using a tableview. 我使用tableview粗化了一些东西。 The basic behavior you are looking for is to add rows when a date is selected (and hide those previously selected). 您正在寻找的基本行为是在选择日期时添加行(并隐藏以前选择的行)。 I did a tableView with a section for each day, and added events below it. 我每天做一个tableView并有一个部分,并在其下添加事件。

I added a TableView from a xib, but should have done it in code for this setup. 我从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

The tricky part is getting the date information, and the customization. 棘手的部分是获取日期信息和自定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM