views:

302

answers:

1

Hello all, I have an app that has a tab bar, each tab contains a separate table. The first table uses core data to persist its entries and checkmarks. The second table on the second tab uses an NSMutableArray to populate it (I would use core data but I would have to pre populate it and this table does not allow that) I would like to persist check marks the same way I do in the first table with core data but something is wrong. The code looks like this:

 -(void)viewDidLoad {
    [super viewDidLoad];
airport = [[NSMutableArray alloc] init];
[airport addObject:@"Passport"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [airport count];
}


// Customize the appearance of table view cells.
- (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];
}

// Set up the cell...
NSString *cellValue = [airport objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
//[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];


NSManagedObject *item = [[self fetchedResultsController] objectAtIndexPath:indexPath];
cell.textLabel.text = [item valueForKey:@"name"]; //CHANGED TO DETAIL


if ([[item valueForKey:@"check"] boolValue]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


NSManagedObject *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];


if ([[selectedObject valueForKey:@"check"] boolValue]) {
    [selectedObject setValue:[NSNumber numberWithBool:NO] forKey:@"check"];
} else {
    [selectedObject setValue:[NSNumber numberWithBool:YES] forKey:@"check"];
}

UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath]; 
if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
    thisCell.accessoryType = UITableViewCellAccessoryCheckmark;  
} else {
    thisCell.accessoryType = UITableViewCellAccessoryNone;
} 
[tableView deselectRowAtIndexPath:indexPath animated:NO];       

}

I believe the line cell.textLabel.text = [item valueForKey@"name"]; is whats causing it. All that I would like for this to do is have the table populated from the array and check marks persisted. Any help is GREATLY appreciated. Thanks in advance.

A: 

This line is replacing the cell text:

cell.textLabel.text = [item valueForKey:@"name"];

that was initially assigned by these lines:

NSString *cellValue = [airport objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

Why are your using the "name" property of "item"?

gerry3
Name is an attribute in an entity called "Hero" That is how my first table works where it checks if the name has a check mark when loading. Is there any way I could persist a check mark with an array or pre loaded list?
Tanner
There are many ways to do what you want including with Core Data. I would use Core Data since you are already using it, but if you want you can persist the checked data array using a property list (plist).
gerry3
How could I use a plist to persist checkmarks? Would the array go away in place of a plist?
Tanner
A plist is a file type. You can write an array to storage as a plist using `writeToFile:atomically:` (and read it with `initWithContentsOfFile:`). Assuming it is Core Data managed objects that are being marked as "checked", you probably want to store an array of the "checked" objects' `objectID` properties (make sure all objects have been saved first). Again though, why not use Core Data for this?
gerry3
In my experience with core data it can not be pre populated. I onced use an Apple Developer Tech support for core data and was told that it isnt. So far im just trying to write to NSUserDefaults siince this is only check mar persistance. The full code can be seen here. Im still unawhere what makes it crash this time.http://stackoverflow.com/questions/2267579/how-to-persist-a-checkmark-with-a-table-populated-by-a-plistI added this question just so people can see the changed code.
Tanner
It definitely can be pre-populated: you can generate a persistant store file during development either with your app or with a mac utility that uses the same data model and then use it as read-only or copy it from the app bundle to the Documents directory and start adding user objects to it. Also, if there are not a lot of objects to pre-populate, you can just generate them manually the first time your app runs and those objects will be persisted going forward.
gerry3
I would like a table to be populated with core data more than anything but this app is in the app store. Can you point me in the right direction if I create a mac utility to populate this? Wont it damage the persistant store for the people that have already downloaded it? Also the table is a few layers into the app. Will this affect anything? Thanks
Tanner