我有一个自定义表格视图单元格。有两个标签。 右侧的标签从实体加载其文本。 (见下图)
-(void)viewWillAppear:(BOOL)animated{
if (self.context == nil)
{
self.context = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:self.context];
[request setEntity:entity];
NSError *error;
NSMutableArray *array = [[self.context executeFetchRequest:request error:&error] mutableCopy];
[self setTableArray:array];
[self.myTableView reloadData];
[super viewWillAppear:YES];
}
我根据文档相应地设置了文本。
当我尝试从不同的实体加载第二个标签文本时,我的问题就在于,所以简单地说,我试过这个:
-(void)viewWillAppear:(BOOL)animated{
if (self.context == nil)
{
self.context = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSFetchRequest *sRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:self.context];
NSEntityDescription *sEntity = [NSEntityDescription entityForName:@"Entity2" inManagedObjectContext:self.context];
[request setEntity:entity];
[sRequest setEntity:sEntity];
NSError *error;
NSError *sError;
NSMutableArray *array = [[self.context executeFetchRequest:request error:&error] mutableCopy];
NSMutableArray *sArray = [[self.context executeFetchRequest:sRequest error:&sError]mutableCopy];
[self setTableArray:array];
[self setSecondTableArray:sArray];
[self.ammoTable reloadData];
[super viewWillAppear:YES];
}
我通过以下方式设置表格View的单元格文本:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identifier = @"identifier";
self.cell = [tableView dequeueReusableCellWithIdentifier:identifier];
Entity2 *entity2 = [self.secondTableArray objectAtIndex:indexPath.row];
Entity *entity = [self.tableArray objectAtIndex:indexPath.row];
self.cell.rightLabel.text = [[entity.brand stringByAppendingString:@" "] stringByAppendingString:entity.model];
self.cell.leftLabel.text = entity2.numberOfRounds;
return self.cell;
}
,当我推动这个观点时,我得到了这个错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
我在这一行收到SIGABRT
错误:
Entity2 *entity2 = [self.secondTableArray objectAtIndex:indexPath.row];
非常感谢任何帮助,谢谢!
答案 0 :(得分:1)
我没有尝试加载2个不同的实体,而只是在原始实体中添加了另一个属性,并导入/使用了实体中新创建的属性来填充正确的标签!