我有一个带标签栏的应用,每个标签都包含一个单独的表格。第一个表使用核心数据来保留其条目和复选标记。第二个选项卡上的第二个表使用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”]; 是什么导致它。我想要做的就是从数组中填充表格并保持复选标记。任何帮助是极大的赞赏。提前谢谢。
答案 0 :(得分:0)
此行正在替换单元格文本:
cell.textLabel.text = [item valueForKey:@"name"];
最初由这些行分配:
NSString *cellValue = [airport objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
为什么你使用“item”的“name”属性?