Я пытаюсь заполнить контроллер Tableview с объектами из 2-х разных массивов, но он выходит из строя и дает эту ошибку «завершающему приложение из-за неперехваченное исключение„NSRangeException“, причина:" - [__NSArrayM objectAtIndex:]: индекс 1 за пределами границ [0 .. 0] "***Заполнение таблицы с 2 массивами
как исправить это? Ниже мой код:
(void)viewDidAppear:(BOOL)animated{
//[super viewDidAppear:<#animated#>];
NSManagedObjectContext *bookmanagedObjectContext = [self managedObjectContext];
NSFetchRequest *bookfetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Book"];
NSPredicate *bookpredicate =[NSPredicate predicateWithFormat:@" bookref.toproject contains[cd]%@",self.projectdb];
[bookfetchRequest setPredicate:bookpredicate];
NSSortDescriptor *booksortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"authorSurname" ascending:YES];
NSArray *booksortDescriptors = [[NSArray alloc]initWithObjects:booksortDescriptor, nil];
[bookfetchRequest setSortDescriptors:booksortDescriptors];
self.BookrefArray = [[bookmanagedObjectContext executeFetchRequest:bookfetchRequest error:nil] mutableCopy];
NSManagedObjectContext *journalmanagedObjectContext = [self managedObjectContext];
NSFetchRequest *journalfetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Journal"];
NSPredicate *journalpredicate =[NSPredicate predicateWithFormat:@" journalref.toproj contains[cd]%@",self.projectdb];
[journalfetchRequest setPredicate:journalpredicate];
NSSortDescriptor *journalsortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"surname" ascending:YES];
NSArray *journalsortDescriptors = [[NSArray alloc]initWithObjects:journalsortDescriptor, nil];
[journalfetchRequest setSortDescriptors:journalsortDescriptors];
self.JournalrefArray = [[journalmanagedObjectContext executeFetchRequest:journalfetchRequest error:nil] mutableCopy];
[self.tableView reloadData];}
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return (self.BookrefArray.count + self.journalrefArray.count);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Journal *myjournal =[self.journalrefArray objectAtIndex:indexPath.row];
[cell.detailTextLabel setText:[myjournal valueForKey:@"journalname"]];
[cell.textLabel setText:[NSString stringWithFormat:@"%@, %@",[myjournal valueForKey:@"surname"],[myjournal valueForKey:@"firstname"]]];
Book *mybook =[self.BookrefArray objectAtIndex:indexPath.row];
// Configure the cell...
[cell.detailTextLabel setText:[mybook valueForKey:@"bookTitle"]];
[cell.textLabel setText:[NSString stringWithFormat:@"%@, %@",[mybook valueForKey:@"authorSurname"],[mybook valueForKey:@"authorOthernames"]]];
return cell;
}
Вам нужно предоставить более подробную информацию о своих массивах - где они инициализированы? – Luke
Я отредактировал код выше – yasmin