Calendars in array out of scope after filtering
I am making a little program that will put some events in a calendar on the iPhone. In the settings I will let the user select wich calendar to use. To present what calendars he can use I pull all calendars from the EKEventStore and sort out those that doesn't allow modifications. Those are subscribed from other websites.
After the filter, wich seems to be OK, the array is reduced from 5 to 3 calendars, all objects in the array are out of scope, and the list in the tableview is blank.
What am I missing?
Edit: The problem erupted when I started with the filtering, thats why I thought that was the problem, but now it seems that the objects go out of scope when the -(NSArray*)availableCalendar returns the array. Do I need to copy it or something?
Image here: http://d.pr/35开发者_JAVA技巧HY
-(NSArray*)availableCalendars{
NSArray *calendars;
EKEventStore *eventDB = [[[EKEventStore alloc]init]autorelease];
calendars = [[[NSArray alloc]initWithArray:[eventDB calendars]]autorelease];
return calendars;
}
- (void)viewDidLoad {
[super viewDidLoad];
allcalendars = [self availableCalendars];
[allcalendars retain];
localCalendars = [[NSMutableArray alloc]initWithArray:allcalendars];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"allowsContentModifications == YES"];
[localCalendars filterUsingPredicate:predicate];
calendarCountInt = localCalendars.count; //When I break the code here, the three objects are 'Out of Scope' and the count is three
}
- (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] autorelease];
}
if (calendarCountInt > 0)
{
cell.textLabel.text = [[localCalendars objectAtIndex:indexPath.row] title] ;
}
else {
cell.textLabel.text = @"No Calendars found";
}
return cell;
}
- (void)dealloc {
[localCalendars release];
[allcalendars release];
[super dealloc];
}
Your code is ... interesting. I'm guessing that you're doing return calendarCountInt;
in your tableView:numberOfRowsInSection:
method. If you are, then that's going to return 0;
when there are no calendars that allow modification, resulting in an empty table.
Here's how I would do it:
// this requires an @property(nonatomic, retain) NSArray *allCalendars
// with a corresponding NSArray *allCalendars ivar
// also, an @property(nonatomic, retain) NSArray *localCalendars
// with a corresponding NSArray *localCalendars ivar
// get all calendars and cache them in an ivar (if necessary)
- (NSArray *)allCalendars {
if (allCalendars == nil) {
EKEventStore *eventDB = [[[EKEventStore alloc] init] autorelease];
[self setAllCalendars:[eventDB calendars]];
}
return allCalendars;
}
// get all modifiable calendars and cache them in an ivar (if necessary)
- (NSArray *)localCalendars {
if (localCalendars == nil) {
NSPredicate *filter = [NSPredicate predicateWithFormat:@"allowsContentModifications == YES"];
[self setLocalCalendars:[[self allCalendars] filteredArrayUsingPredicate:filter]];
}
return localCalendars;
}
// there's only one section
- (NSUInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
return 1;
}
// show the number of modifiable calendars, or 1 (if there are no modifiable calendars)
- (NSUInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSUInteger)section {
NSArray *local = [self localCalendars];
return ([local count] > 0) ? [local count] : 1;
}
// set up the cell
- (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] autorelease];
}
if ([[self localCalendars] count] > 0) {
cell.textLabel.text = [[[self localCalendars] objectAtIndex:[indexPath row]] title];
} else {
cell.textLabel.text = @"No calendars found";
}
}
- (void)dealloc {
[localCalendars release], localCalendars = nil;
[allCalendars release], allCalendars = nil;
[super dealloc];
}
精彩评论