我有一个自定义表格视图单元格。带有两个标签。右侧的标签从实体加载其文本。(见下图)
-(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];
}
我通过以下方式设置表格视图的单元格文本:
-(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];
任何帮助将不胜感激,谢谢!