【发布时间】:2014-08-02 20:17:22
【问题描述】:
我正在 Xcode 上制作一个 coredata 应用程序。我有几个实体,它们都使用自己的原型单元格样式在自己的表格中填充单元格。我想查看一个主表上的所有实体,并发送每个实体以填充其匹配的单元格。
我认为最好的方法是创建一个抽象实体并使用 if 语句为每个实体声明单元标识符。我可能是错的,因为它还没有奏效。这是我所拥有的:
在 viewDidLoad 中:
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"MyAbstractEntity" inManagedObjectContext:_managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
关系在数据模型中建立。这是试图识别子实体的表:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
if ([NSEntityDescription entityForName:@“Animals” inManagedObjectContext:_managedObjectContext])
{
identifier = @“AnimalsCell";
AnimalsCell *animalsView = (AnimalsCell *)[tableView dequeueReusableCellWithIdentifier:@"AnimalsCell" forIndexPath:indexPath];
Animals *animals = (Animals *)[reportArray objectAtIndex:indexPath.row];
animalsView.descriptionTextField.text = [animals description];
return animalsView;
}
if ([NSEntityDescription entityForName:@“Plants” inManagedObjectContext:_managedObjectContext])
{
identifier = @“PlantsCell";
PlantsCell *animalsView = (PlantsCell *)[tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
Plants *plants = (Plants *)[reportArray objectAtIndex:indexPath.row];
plantsView.flowerTextField.text = [plants flowerDetail];
return plantsView;
}
return 0;
}
如果我输入动物,它会显示在概览表中。如果我输入植物,它会崩溃,因为它试图将植物数据放入动物细胞中,这意味着我的标识符 if 语句无法正常工作。这是我第一次尝试显示来自多个实体的数据,而且我从未使用过抽象实体,所以我可能做错了。非常感谢,伙计们!
【问题讨论】:
标签: objective-c uitableview core-data entity fetch