开发者

Is there anyway to populate a UITableView with an array but persist checkmarks?

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:(UITableVie开发者_开发问答w *)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.


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"?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜