我的目标是在UIViewController中设置两个(或更多)tableView。我正在使用Vurig日历,但它没有tableviews,所以我试图自己做。一个tableView来自外部,看起来不错。当我尝试将委托和数据源设置为自身时,另一个中断。 代码是:
#import <UIKit/UIKit.h>
#import "VRGCalendarView.h"
#import "ListViewController.h"
//#import "DayTableViewController.h"
@interface CalendarViewController : UIViewController <VRGCalendarViewDelegate, UITableViewDelegate,UITableViewDataSource>
{
VRGCalendarView *calendar;
// DayTableViewController *dayViewController;
ListViewController *listViewController;
}
@property (nonatomic, weak) IBOutlet UITableView *monthView;
@property (nonatomic, weak) IBOutlet UITableView *listView;
@property (nonatomic, weak) IBOutlet UISegmentedControl *segControl;
- (IBAction)segmentedValueChanged:(UISegmentedControl *)sender;
@end
CalendarViewController.m:
#import "CalendarViewController.h"
#import <EventKit/EventKit.h>
@interface CalendarViewController ()
@property (nonatomic, strong) EKEventStore *eventStore;
@property (nonatomic, strong) NSMutableArray *events;
@end
@implementation CalendarViewController
@synthesize eventStore;
@synthesize events;
@synthesize monthView = _monthView;
@synthesize listView = _listView;
-(void)viewDidLoad{
[super viewDidLoad];
[_segControl addTarget:self action:@selector(segmentedValueChanged:) forControlEvents:UIControlEventValueChanged];
[_segControl setSelectedSegmentIndex:1];
//kalendar
calendar = [[VRGCalendarView alloc] init];
calendar.delegate=self;
[self.view addSubview:calendar];
//lista
if (listViewController == nil)
{
listViewController = [[ListViewController alloc] init];
}
[_listView setDataSource:listViewController];
[_listView setDelegate:listViewController];
listViewController.view = listViewController.tableView;
_listView.hidden = YES;
/*
if (dayViewController == nil)
{
dayViewController = [[DayTableViewController alloc] init];
}
[monthView setDataSource:dayViewController];
[monthView setDelegate:dayViewController];
dayViewController.view = dayViewController.tableView;
*/
//eventi
NSDate *startDate = [NSDate date];
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:(60 * 60 * 24)];
// Create the predicate from the event store's instance method
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
endDate:endDate
calendars:nil];
NSArray *results = [self.eventStore eventsMatchingPredicate:predicate];
NSMutableArray *eventsi = [[NSMutableArray alloc] initWithArray:results];
self.events = eventsi;
[_monthView setDataSource:self];
[_monthView setDelegate:self];
_monthView.hidden = NO;
//[monthView reloadData];
}
-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated {
if (month==[[NSDate date] month]) {
// prazan array za prikaz
NSMutableArray *dates = [[NSMutableArray alloc] init];
NSDate* endDate = [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantFuture] timeIntervalSinceReferenceDate]];
NSPredicate *fetchCalendarEvents = [eventStore predicateForEventsWithStartDate:[NSDate date] endDate:endDate calendars:nil];
NSArray *eventList = [eventStore eventsMatchingPredicate:fetchCalendarEvents];
for (int i = 0; i < [eventList count]; i++)
{
NSLog(@"Event Title:%@", [[eventList objectAtIndex:i] title]);
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
NSString *dayInMonthStr = [dateFormatter stringFromDate:today];
// tu dobijemo integer od tog dana
int dayInMonth = [dayInMonthStr intValue];
// dodamo taj datum u array
[dates addObject:[NSString stringWithFormat:@"%i", dayInMonth]];
}
// na kraju prikazemo
[calendarView markDates:dates];
}
}
-(void)calendarView:(VRGCalendarView *)calendarView dateSelected:(NSDate *)date {
NSLog(@"Selected date = %@",date);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.events count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
EKEvent * event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = event.title;
cell.detailTextLabel.text = event.calendar.title;
if (event.calendar.CGColor) {
cell.detailTextLabel.textColor = [UIColor colorWithCGColor:event.calendar.CGColor];
}
return cell;
}
//segment part
- (IBAction)segmentedValueChanged:(UISegmentedControl *)sender
{
switch (sender.selectedSegmentIndex) {
case 0:
{
_listView.hidden = NO;
[_listView reloadData];
calendar.hidden = YES;
_monthView.hidden = YES;
}
break;
case 1:
calendar.hidden = NO;
_monthView.hidden = NO;
[_monthView reloadData];
_listView.hidden = YES;
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
在IB中连接了monthView(IBOutlet,源代码和委托与CalendarViewController)。我看不出我做错了什么。