【发布时间】:2015-03-25 14:45:45
【问题描述】:
我正在尝试使用来自 2 个不同数组的对象填充 tableview 控制器,但它崩溃并出现此错误 “由于未捕获的异常'NSRangeException'而终止应用程序,原因:' - [__NSArrayM objectAtIndex:]:索引1超出范围[0 .. 0]”***
我该如何解决这个问题?以下是我的代码:
(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;
}
【问题讨论】:
-
您需要提供有关您的数组的更多详细信息 - 它们在哪里初始化?
-
我编辑了上面的代码
标签: objective-c iphone uitableview