【发布时间】:2010-02-15 04:07:45
【问题描述】:
我有一个带有标签栏的应用,每个标签都包含一个单独的表格。第一个表使用核心数据来保存其条目和复选标记。第二个选项卡上的第二个表使用 NSMutableArray 来填充它(我会使用核心数据,但我必须预先填充它,而这个表不允许这样做)我想以与第一个相同的方式保留复选标记包含核心数据的表,但有问题。代码如下所示:
-(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];
}
我相信这行 cell.textLabel.text = [item valueForKey@"name"]; 是什么原因造成的。我想要做的就是从数组中填充表格并保留复选标记。任何帮助是极大的赞赏。提前致谢。
【问题讨论】:
标签: core-data uitableview persistence nsmutablearray