Why my tableView did not display anything - objective-c

i want select over 50 in the first section,and others in the second section ,but there is no value display in tableView ,and when i use NSlog("%#",self.over50) ,there is no reaction
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" ];
NSArray* items = [[BNRItemStore shareStore] allItems];
self.itemsUnder50 = [[NSMutableArray alloc] init];
self.itemsOver50 = [[NSMutableArray alloc] init];
for (BNRItem *item in items) {
if (indexPath.section ==0) {
[self.itemsOver50 addObject:item];
cell.textLabel.text = [self.itemsOver50[indexPath.row] description];
NSLog(#"%#",self.itemsUnder50);
}
else if(indexPath.section == 1){
[self.itemsUnder50 addObject:item];
cell.textLabel.text = [self.itemsUnder50[indexPath.row] description];
}
}
return cell;
}

connect the datasource and delegate of the tableview to the viewController

Related

multiple checkmarks from

Tutorial I am following: http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/
I have created a tableview with 16 cells. When I select a row, it will show checkmark on it.
But when I scroll the tableview, there is also a checkmark showing on another cell further down the list. This repeats for any cell selected.
#import "FlightChecklistViewController.h"
#interface FlightChecklistViewController ()
#end
#implementation FlightChecklistViewController
{
NSArray *tableData;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize table data
tableData = [NSArray arrayWithObjects:#"Egg Benedict", #"Mushroom Risotto", #"Full Breakfast", #"Hamburger", #"Ham and Egg Sandwich", #"Creme Brelee", #"White Chocolate Donut", #"Starbucks Coffee", #"Vegetable Curry", #"Instant Noodle with Egg", #"Noodle with BBQ Pork", #"Japanese Noodle with Pork", #"Green Tea", #"Thai Shrimp Cake", #"Angry Birds Cake", #"Ham and Cheese Panini", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:#"You've selected a row" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display Alert Message
[messageAlert show];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
Any suggestions?
You need to store the information about the rows indexpaths, that were selected, somehow.
And populate your cell according to it.
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) NSMutableArray *selectedCells;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedCells = [NSMutableArray array];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *unifiedID = #"aCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
}
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
//if the indexPath was found among the selected ones, set the checkmark on the cell
cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
//if a row gets selected, toggle checkmark
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
[self.selectedCells removeObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[self.selectedCells addObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}
#end
you will find the complete example code on github
The problem is that cells are reused. So, if you add a checkmark accessory view to a cell further up it'll appear again when the cell is reused further down. You should save which ones are checkmarked in an array somewhere that correlates to the rows of the table when you add/remove a checkmark. Then, when you give the table view a new cell you can determine whether or not it needs a checkmark and set that up.
I had the same issue recently with one of my apps, and I fixed it by doing this:
#property (nonatomic, strong) NSArray *list;
- (void)viewDidLoad
{
[super viewDidLoad];
self.list = [[NSArray alloc] initWithObjects:#"foo", #"bar", nil];
}
- (NSString *)SettingsPlist
{
NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *PlistPath = [paths stringByAppendingPathComponent:#"Settings.plist"];
return PlistPath;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[self list] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *contentForThisRow = [[self list] objectAtIndex:[indexPath row]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[self SettingsPlist]];
NSString *row = [NSString stringWithFormat:#"%d",indexPath.row];
if([[dict objectForKey:row]isEqualToString:#"0"])
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:[self SettingsPlist]];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *row = [NSString stringWithFormat:#"%d",indexPath.row];
if(cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *on = #"1";
[plist setObject:on forKey:row];
[plist writeToFile:[self SettingsPlist] atomically:YES];
}
else if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
NSString *off = #"0";
[plist setObject:off forKey:row];
[plist writeToFile:[self SettingsPlist] atomically:YES];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Check marks are unchecked when scroll the tableview

i am using tableview and display data. in the table view all checkMark items sent mail. but when scroll the table check marks are unchecked. i don't want un check when scroll the table. please help me. i am developing below code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = #"CustomCellIdentifier ";
CaptureCell *cell = (CaptureCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CaptureCell"
owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[CaptureCell class]])
cell = (CaptureCell *)oneObject;
}
if ([[[dataArray objectAtIndex:indexPath.row] objectForKey:#"CaptureId"]isEqualToString:[dict objectForKey:[[dataArray objectAtIndex:indexPath.row] objectForKey:#"CaptureId"]]])
{
cell.confirmMail.image = [UIImage imageNamed:#"mailConfirm.png"];
}
NSString *input;
input = [[dataArray objectAtIndex:indexPath.row]objectForKey:#"amount"];
[input stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//NSLog(#"after removing white spaces %#",input);
double price;
NSString *numberAsString = nil;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[numberFormatter setLocale:gbLocale];
[gbLocale release];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
if ([[input stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]length]!=0)
{
NSLog(#"the input is***** %#",input);
price=[[input stringByReplacingOccurrencesOfString:#"$" withString:#""] doubleValue];
NSLog(#"the price is %f",price);
numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:price]];
NSLog(#"the numberAsString...... %#",numberAsString);
}
[numberFormatter release];
cell.eventNameLbl.text = [[dataArray objectAtIndex:indexPath.row]objectForKey:#"expenseType"];
cell.expenTypeLbl.text = [[dataArray objectAtIndex:indexPath.row]objectForKey:#"empName"];
cell.dataLbl.text = [[dataArray objectAtIndex:indexPath.row]objectForKey:#"Date"];
cell.amountLbl.text = numberAsString;
cell.billImage.image = [UIImage imageWithData:[[dataArray objectAtIndex:indexPath.row]objectForKey:#"image"]];
//[cell.textLabel setText:[NSString stringWithFormat:#"Row %d", indexPath.row]];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
NSIndexPath* selection = [tableView indexPathForSelectedRow];
if (selection && selection.row == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
// if([[[captureArray objectAtIndex:indexPath.row]objectForKey:#"eventName"]isEqualToString:[[selectedData objectAtIndex:indexPath.row]objectForKey:#"eventName"]])
// NSLog(#"Equal");
//[cell setEditing:YES animated:YES];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *selected = [dataArray objectAtIndex:indexPath.row];
if([tableView isEditing] == YES)
{
AppDelegate *sharedDelegate = [[UIApplication sharedApplication]delegate];
sharedDelegate.isEditCapture = YES;
sharedDelegate.findDate = YES;
NSString *imgDataStr = [[NSString alloc]initWithData:[[dataArray objectAtIndex:indexPath.row]objectForKey:#"image"] encoding:NSUTF8StringEncoding];
NSLog(#"imageStr%#",imgDataStr);
ExpensesCaptureViewController_iPhone *nextController = [[ExpensesCaptureViewController_iPhone alloc] initWithNibName:#"ExpensesCaptureViewController_iPhone" bundle:nil];
nextController.captureId = [[dataArray objectAtIndex:indexPath.row] objectForKey:#"CaptureId"];
[self.navigationController pushViewController:nextController animated:YES];
[imgDataStr release];
[nextController release];
if([selectedData count]>0)
{
[selectedData removeObject:selected];
}
}
else {
if ([[[dataArray objectAtIndex:indexPath.row] objectForKey:#"CaptureId"]isEqualToString:[dict objectForKey:[[dataArray objectAtIndex:indexPath.row] objectForKey:#"CaptureId"]]])
{
NSLog(#"sent items");
}
else {
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedData addObject:selected];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedData removeObject:selected];
}
}
}
NSLog(#"DataObjects %d %#",[selectedData count] ,[[dataArray objectAtIndex:indexPath.row] objectForKey:#"eventName"]);
//[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
The problem that you scroll your tableView when - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method is called and you set
NSIndexPath* selection = [tableView indexPathForSelectedRow];
if (selection && selection.row == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
that means when you will scroll indexPath will be for the current row only so the checkmark at the current row will still checked but the all the other rows will enter the else condition and the checkmark will be removed.
To fix it you should make a do the following:
define the following property:
#property (nonatomic)BOOL isViewIsLoadedForTheFirstTime;
-(void)viewDidLoad
{
self.isViewIsLoadedForTheFirstTime = YES;
}
then replace these lines in the (cellForRow) method
if (selection && selection.row == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
to the following lines:
if (self.isViewIsLoadedForTheFirstTime) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.isViewIsLoadedForTheFirstTime = NO;
}
This will make you set the check mark only when the view loaded for the first time the when you will scroll it won't change.
I hope that will help you

Objective-C, iOS SDK, NSMutableArray, add elements and save data

Basically, every time I enter something in my textfield and save it to the table, the previous entry will be replaced by the new one. How do I add elements in a mutable array and save the last entry? I tried
[tabledata addObject....
and in the entry
tabledata.lastObject objectAtIndex...
but that didn't work.
here is what I have:
-(void)viewDidLoad
{
[super viewDidLoad];
titlestring = [[NSUserDefaults standardUserDefaults] objectForKey:#"titlehomework" ];
detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:#"detailshomework"];
tabledata = [[NSMutableArray alloc] initWithObjects:titlestring, nil];
tablesubtitles = [[NSMutableArray alloc] initWithObjects:detailsstring, nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [tabledata count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"homeworkcell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"homeworkcell"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.textLabel.backgroundColor = [ UIColor clearColor ];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
//-----------------------------------------START----------------------------Set image of cell----
cellImage = [UIImage imageNamed:#"checkboxblank.png"];
cell.imageView.image = cellImage;
//--------------------------------------------END---------------------------end set image of cell--
return cell;
}
Without seeing more code, the best I can do is suggest something like this
[tabledata addObject:newTitle];
[tablesubtitles addObject:newSubtitle];
[tableView reloadData];
This assumes both newTitle and newSubtitle are the NSStrings you wish to add and that tableView is the pointer to the UITableView.

cannot delete the right cell in the table

I am making a small app. And having a trouble right now. Trouble by deleting a table customized cell.
it keep removing the top cell instead of the right selected cell. I delete cell number 20, it still delete the cell number 1. I don't know why. please help me out. Really appreciate.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[sortedArray removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}
and here is the array:
- (void)viewDidLoad
{
[super viewDidLoad];
if (detail == nil) {
detail = [[UrlDetail alloc] init];
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
self.arrayData = [NSMutableArray arrayWithContentsOfFile:path];
NSMutableArray *filterArr = [self filterArray];
sortedArray = [[NSMutableArray alloc] initWithArray:filterArr copyItems:YES];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(actionAddNewURL:)] autorelease];
}
and here is the filter function:
-(NSMutableArray *)filterArray
{
NSMutableArray *filterArr = [[NSMutableArray alloc] init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[filterArr addObject:tempArray];
[tempArray release];
for (NSDictionary *item in arrayData) {
if ([tempArray count]==0)
{
[tempArray addObject:item];
}
else
{
NSDictionary *anItem = [tempArray objectAtIndex:0];
NSString *first = [[anItem objectForKey:#"url"] substringToIndex:1];
NSString *last = [[item objectForKey:#"url"] substringToIndex:1];
if ( [first isEqualToString:last])
{
[tempArray addObject:item];
} else
{
tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:item];
[filterArr addObject:tempArray];
[tempArray release];
}
}
}
// NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:filterArr copyItems:YES];
return filterArr;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([sortedArray count]>0){
NSLog(#"number of section: %d", [sortedArray count]);
return [sortedArray count];
}
return 0;
}
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
if (isTab) {
if ([self.sortedArray count] > section) {
NSDictionary *dictionary = [[sortedArray objectAtIndex:section] objectAtIndex:0];
NSString *string = [dictionary objectForKey:#"url"];
return [NSString stringWithFormat:#"%#", [[string substringToIndex:1] uppercaseString]];
}
else return nil;
} else
{
return nil;
}
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = myOwnCell;
}
NSDictionary *dataItem = [[sortedArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.urlName.text = [dataItem objectForKey:#"url"];
cell.titleLabel.text = [dataItem objectForKey:#"title"];
cell.urlName.font = [UIFont italicSystemFontOfSize:14.0];
cell.imageIcon.image = [UIImage imageNamed:[dataItem objectForKey:#"image"]];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// Configure the cell.
return cell;
}
Try this one if it works:
- (void) tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *key = [[sortedArray allKeys] objectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[sortedArray removeObjectForKey:key];
}
}

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/35HY
-(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];
}